1

I have a text file in which the newline is specified by \r and not \n. So while using mgetl() in Scilab or readlines() in python, the whole file is read. The file is really huge and I want to avoid reading it entirely. How to do that?

Interestingly, when I tried universal newline support as recommended in this post, the command converted all '\r' characters to '\n' and then read all the lines! So the problem remains unsolved for me both in Scilab and python. The issue is that I need solution in both languages! :/

Please help..

Community
  • 1
  • 1
Mi2n
  • 11
  • 2

1 Answers1

0

In Scilab language, you can use the mfscanf() function as follows:

cr = ascii(13);
txt = "abcdefgh" + cr + "ijkl" + cr + cr + "mnopq";
mputl(txt, "test.txt")

fid = mopen("test.txt", "r");
row = 0;
while row <> []
    row = mfscanf(fid, "%s\r")
end
mclose(fid)

This will read one by one the rows ended with / separated by \r:

-->     while row <> []
  >         row = mfscanf(fid, "%s\r")
  >     end
 row  = 
  "abcdefgh"
 row  = 
  "ijkl"
 row  = 
  "mnopq"
 row  = 
    []

Note that empty rows are ignored (consecutive ascii(13) are "merged").

S. Gougeon
  • 791
  • 3
  • 16