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

조회 수: 6 (최근 30일)
vikas
vikas 2012년 4월 27일
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.
  댓글 수: 3
Jan
Jan 2012년 4월 28일
There is no ASCII value for 256.
Walter Roberson
Walter Roberson 2012년 4월 28일
There are no ASCII values beyond 127.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by