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

1 view (last 30 days)
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 !!

Answers (1)

Walter Roberson
Walter Roberson on 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.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!