potential issue with fscanf new line character, the newline character is /n coming from a c background i was trying \n.

43 visualizaciones (últimos 30 días)
fid = fopen("Files.txt","r");
string = fscanf(fid,"%s\n")
string = 'test1.txttest2.txttest3.txttest4.txttest5.txttest6.txttest7.txttest8.txttest9.txttest10.txt'
fclose(fid);
fid = fopen("Files.txt","r");
string = fscanf(fid,"%s/n")
string = 'test1.txt'
fclose(fid);
fprintf("Not consistent newline char\n(Used the backslash here not forward slash)\n");
Not consistent newline char (Used the backslash here not forward slash)
  3 comentarios
Lachlan
Lachlan el 5 de Abr. de 2024 a las 12:19
The desired result is the same as string = fscanf(fid,"%s/n") I beleive this is a bug because clearly the newline character is \n, as is the case in fprintf and in C's implementation of fscanf.
Stephen23
Stephen23 el 5 de Abr. de 2024 a las 13:24
Editada: Stephen23 el 5 de Abr. de 2024 a las 13:25
"The desired result is the same as string = fscanf(fid,"%s/n") I beleive this is a bug because clearly the newline character is \n, as is the case in fprintf and in C's implementation of fscanf."
Nothing you have shown looks like a bug, nor does your expectation match the documented behavior of FSCANF:
  • your example with "%s\n" reads some non-whitespace text until the end of the line, then reads a newline, and then repeats this many times (because there are no non-matching characters to stop the parsing).
  • your example with "%s/n" reads some non-whitespace text and then stops because there is no literal match for the literal characters "/" and "n".
This behavior is explained in the FSCANF documentation:
particularly where it explains at the topc of the page "The fscanf function reapplies the format throughout the entire file and positions the file pointer at the end-of-file marker. If fscanf cannot match formatSpec to the data, it reads only the portion that matches and stops processing."
So far everything is exactly as documented and expected. So far you have not provided an explanation of why you expect some behavior different to that given in the FSCANF documentation.

Iniciar sesión para comentar.

Respuestas (1)

Image Analyst
Image Analyst el 3 de Abr. de 2024 a las 14:37
Maybe you might want to try readlines instead.
  2 comentarios
Lachlan
Lachlan el 5 de Abr. de 2024 a las 12:20
I coicidently discovered what I need to do to get what I wanted namely use /n as the newline character. The reason I raised this question is because I beleive this is a bug.
Image Analyst
Image Analyst el 6 de Abr. de 2024 a las 18:10
Why? I don't know anyone who uses or assumes /n as the new line character. Everyone uses \n as far as I know. So again, why do you want to use /n? And it's not a bug because like @Stephen23 said, it quits reading if it doesn't see a /n -- that's normal and expected.

Iniciar sesión para comentar.

Categorías

Más información sobre Low-Level File I/O en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by