Problem while Writing data into and Reading data ifrom a text file

6 Ansichten (letzte 30 Tage)
vikas
vikas am 27 Apr. 2012
I desperately need some help on this issue I am trying to perform Encryption and Decryption operation using a particular technique. The code is working perfectly. However i
am facing this problem; Once encryption process is performed the encrypted data is written into a file as shown below:
id=fopen('C:\Users\HOME\Desktop\I.txt','w');
fwrite(id,final);
fclose(id);
FINAL is an array of ASCII characters in the range 1 to 256
and this is what i got as an answer for FINAL= xcpucmmucpxcfmc|ppxdpucpxcba
The file I.txt should have contained the same thing but it contained: xcpucmm€ucpxcfmc|ppxdpucpxcba
In the DECRYPTION part i read the file this way:
fid=fopen('C:\Users\HOME\Desktop\I.txt','r');
f=fscanf(dfid,'%c');
fclose(fid);
I proceded by using f (which contains the cipher text) in the decryption process only to get a wrong plaintext.
When i removed all the above codes and simply copied the ciphertext variable FINAL into the variable f in the decryption
process, i got the correct answer i.e
f=final;
My requirement is that both encryption and decryption process must use a text file.
Kindly Help Me !!

Antworten (1)

Walter Roberson
Walter Roberson am 27 Apr. 2012
If you use fwrite() to write the file, you should probably use fread() to read the file.
Question: is "final" an array of characters in the range 1 to 256, or in the range 0 to 255? The 256 would give you problems.
  3 Kommentare
Jan
Jan am 28 Apr. 2012
There is no ASCII value for 256.
Walter Roberson
Walter Roberson am 28 Apr. 2012
There are no ASCII values beyond 127.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Low-Level File I/O finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by