fprintf and fread, character beyond ascii in 1 byte

2 次查看(过去 30 天)
Hi everyone. I'd like to ask if there is a way to write characters beyond 127 with 1 byte? I got a file which was saved in such a way, But I can not write back in the same way. So to speak, if I use "fread" to read in the file byte by byte, at some of them it is a number larger than 127. As shown in the code, if I write a character by 128, it became a 2 bytes (fread get back 2 numbers). How would I write it in order to get also 1 byte 128 back?
fid=fopen('test','w')
fprintf(char(128))
fclose(fid)
fid=fopen('test','r')
test=fread(fid)
fclose(fid)

采纳的回答

Walter Roberson
Walter Roberson 2016-3-23
Your test code forgot to mention fid in the fprintf() so it did not output anything.
Remember that when you use fprintf() with the syntax fprintf(fid, A_String) that the values in A_String are treated as being the format specifier and so are subject to interpretation. The exception to this is if you are sending to a serial device or instrument, in which ase fprintf(fid, A_String) is equivalent to fprintf(fid, '%s\n', A_String) . If you want the exact string sent, you should use fwrite(fid, A_String) instead of fprintf(fid, A_String)
Anyhow, what you might be running into is that fopen() with so few parameters opens the file with the default character encoding for your system. You should fopen() with 4 parameters:
fid=fopen('test','w', 'n', 'ISO-8859-1')
(Note: the third parameter, 'n' in this example, cannot be skipped or left at '' or [] if you need to provide the encoding.)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Propagation and Channel Models 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by