Filter löschen
Filter löschen

open file, read a line then, rewind a line

17 Ansichten (letzte 30 Tage)
Sam
Sam am 30 Nov. 2012
after fgetl(fid), the file position indicatior will be moved to the next line in the file, how could I rewind the indicator back 1 line in the file so when I call fgetl(fid) the second time it would return the same line?
  2 Kommentare
Azzi Abdelmalek
Azzi Abdelmalek am 30 Nov. 2012
What is your goal?
Sam
Sam am 30 Nov. 2012
say this is the text file i want to read
my line 1
my line 2
my line 3
my line 4
say after some steps, fgetl(fid) would return 'my line 3', then what should I do so when I call fgetl(fid) after this, it would still return 'my line 3'?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Matt J
Matt J am 30 Nov. 2012
Bearbeitet: Matt J am 30 Nov. 2012
Use FTELL to measure the position in bytes of each line. Then use FREWIND and/or FSEEK to go to the desired position.
  1 Kommentar
Walter Roberson
Walter Roberson am 30 Nov. 2012
Technical note:
If a file is opened with 'rt', which should be used for text files, then the value returned by ftell() is an arbitrary "token" rather than a file offset in bytes. Using the same token will get you to the same position, but you cannot do computations on the tokens.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Low-Level File I/O finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by