Writing Text to a new Text file from a Variable having Text

1 view (last 30 days)
I am working with images. I have a variable d in which I stored text from a image. The value of variable d is coming right but when I am writing this variable to a new text file it is not coming right. This is the idea. please tell what I am doing wrong:
d = char(I);
disp('Decrypted message is:');
disp(d);
fid = fopen('decr.txt', 'w');
fwrite(fid, d);
fclose(fid);

Accepted Answer

Guillaume
Guillaume on 27 Nov 2014
Edited: Guillaume on 27 Nov 2014
You may want to open your file with 'wt' instead if '|w|'. That will ensure that any end of line is properly formatted for your operating system (some OSes use '\n\r' for end of line, others just '\n' or '\r').
Use fprintf rather than fwrite to write to text files, fwrite is for writing to binary files:
fprintf(fid, '%s\n', d);
If you really want to use fwrite, you need to tell it you want to write characters not bytes:
fwrite(fid, d, 'char');
  5 Comments
Guillaume
Guillaume on 27 Nov 2014
Edited: Guillaume on 27 Nov 2014
The last character of each line is going to be the character of the value of the last pixel of each row of your original image, since your image is 128 columns and you only replace the first 127 columns with your text.
It looks like your doing some sort of steganography. If that is the case, your algorithm doesn't look right to me. Effectively, you're completely replacing the pixels of the original image with the ASCII values of your text. If you need help with that, however, you need to start a new question. It is a completely different topic to this one.
GodGOD
GodGOD on 27 Nov 2014
Edited: GodGOD on 27 Nov 2014
ok thanks alot. yes algo is weird. I am trying to do steganography. I am a beginner so that why algo is not right. I am trying to clear my concepts. You helped me alot thank You

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type 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!