How do I detect line number while reading a file?

조회 수: 7 (최근 30일)
Clay Fulcher
Clay Fulcher 2020년 6월 14일
댓글: Clay Fulcher 2020년 6월 15일
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

채택된 답변

per isakson
per isakson 2020년 6월 15일
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
Walter Roberson
Walter Roberson 2020년 6월 15일
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 2020년 6월 15일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by