Writing ASCII symbols by using fprintf

Hello, Everybody, I will be grateful if you can help me... I have opened a .dat file with fopen and write on it by fprintf to write some numbers. At the end of each line i should put ASCII No13(CR)+No10(LF) and at the end of the file, i should write ASCII No26(EOF). these are the criterion of the program who will read this file and are mandatory. As this file is almost 100000 line i should write this file automatically with MATLAB. How i can put ASCII No13(CR)+No10(LF) automatically at the end of each line and the same on for ASCII No26(EOF)at the end of file by using frprintf?

2 comentarios

Stephen23
Stephen23 el 14 de Ag. de 2018
Editada: Stephen23 el 14 de Ag. de 2018
"ASCII No26(EOF)."
ASCII does not have an "End Of File" character. Did you mean:
  • The 26th character is "End of Medium", abbreviated EM, and has decimal value 25, hex value 19.
  • the 27th character is "Substitute", abbreviated SUB, and has decimal value 26, hex value 1A.
Which of these do you need?
ONK
ONK el 15 de Ag. de 2018
Editada: ONK el 15 de Ag. de 2018
Dear Stephene Thanks for your reply.i will go to try your guidence. In any way thank you so much. I am in contact with you and sure accept your answer(sorry i left my office yesterday).

Iniciar sesión para comentar.

 Respuesta aceptada

Stephen23
Stephen23 el 14 de Ag. de 2018
Editada: Stephen23 el 14 de Ag. de 2018
Windows only:
[fid,msg] = fopen('file.txt','wt'); % open in text mode!
assert(fid>=3,msg)
fprintf(fid,'...\n',yourArray.'); \n auto converted to \r\n.
fprintf(fid,'\x19') % add char(25), ASCII "End of Medium".
fclose(fid);
All OS's, including Windows:
[fid,msg] = fopen('file.txt','w'); % open in binary mode!
assert(fid>=3,msg)
fprintf(fid,'...\r\n',yourArray.'); % explicit \r\n.
fprintf(fid,'\x19') % add char(25), ASCII "End of Medium".
fclose(fid);
Notes:
  • For Windows OS in text mode, |

5 comentarios

Stephen23
Stephen23 el 14 de Ag. de 2018
Editada: Stephen23 el 14 de Ag. de 2018
@ONK: I simplified your code down a bit to this:
Energy = rand(566,1); % fake data
%
[RIM,msg] = fopen('RIM.txt','wt');
assert(RIM>=3,msg)
for k = 1:566
fprintf(RIM, '%d 40 %7E 0 0 0 1.000 0 0\n',k,Energy(k));
end
fprintf(RIM,'\x19');
fclose(RIM);
and then generated a file (attached). The file has \r\n at the end of each line and EndOfMedium at the end of the file, exactly as you specified. Here it is viewed in Notepad++ ( \r\n are indicated by CR and LF):
So that code is working fine. I suspect that your Very Special Program requires that last line to have just the EM marker, and not the \r\n characters. Perhaps try something like this:
[RIM,msg] = fopen('RIM.txt','wt');
assert(RIM>=3,msg)
tmp = '';
for k = 1:566
fprintf(RIM,tmp)
fprintf(RIM, '%d 40 %7E 0 0 0 1.000 0 0',k,Energy(k));
tmp = '\n';
end
fprintf(RIM,'\x19');
fclose(RIM);
which gives this at the end of the file:
Stephen23
Stephen23 el 14 de Ag. de 2018
@ONK: please upload any files here by clicking the paperclip button.
Stephen23
Stephen23 el 14 de Ag. de 2018
Editada: Stephen23 el 14 de Ag. de 2018
That file has Substitute (aka SUB) at the end of the file:
Change the \x19 character format string to \x1A. This worked for me:
Energy = rand(566,1); % fake data
%
[RIM,msg] = fopen('RIM.txt','wt');
assert(RIM>=3,msg)
for k = 1:566
fprintf(RIM, '%d 40 %7E 0 0 0 1.000 0 0\n',k,Energy(k));
end
fprintf(RIM,'\x1A'); % <----- change this format!
fclose(RIM);
ONK
ONK el 14 de Ag. de 2018
One question, how do you produce two previous images that show CR LF and EM characters? which editor are you using that shows this characters?
Stephen23
Stephen23 el 14 de Ag. de 2018
Editada: Stephen23 el 14 de Ag. de 2018
@ONK: I already told you that in my earlier comment, where I wrote this (including a link!):
"Here it is viewed in Notepad++"
If my answer helped you to resolve your original question, then please remember to accept it. Accepting answers is an easy way to show your thanks to the volunteers who help you on this forum.

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Versión

R2018a

Etiquetas

Preguntada:

ONK
el 14 de Ag. de 2018

Abierta de nuevo:

el 16 de Ag. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by