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

1 次查看(过去 30 天)
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 !!

回答(1 个)

Walter Roberson
Walter Roberson 2012-4-27
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.

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by