MATLAB Answers

How do I detect line number while reading a file?

6 views (last 30 days)
Clay Fulcher
Clay Fulcher on 14 Jun 2020
Commented: Clay Fulcher on 15 Jun 2020
Is there a way to detect the line number while reading a text file? ftell will give a pointer in the file, but not the line number.
Thanks

  0 Comments

Sign in to comment.

Accepted Answer

per isakson
per isakson on 15 Jun 2020
There is no function that takes the output of ftell() and returns the line number.
One has to keep track of the line number with user code. However, that is hard to combine with the use of fseek().

  2 Comments

Walter Roberson
Walter Roberson on 15 Jun 2020
The I/O systems used by Windows, Mac, and Linux, do not keep track of the line number. Anything keeping track of the line number would have to be layered on top of the I/O system.
The situation is more tricky than it first appears, since repositioning is permitted, and since it is (generally speaking) permitted to write in the middle of a file (which can change the line numbering for anything after that point), and multiple processes can write into the same file.
Clay Fulcher
Clay Fulcher on 15 Jun 2020
Thanks Isakson and Walter. I guess I'll have to put a line counter in my code and keep track of the lines read that way.

Sign in to comment.

More Answers (0)


Translated by