Filter löschen
Filter löschen

How do I go to a previous line when reading a text file?

27 Ansichten (letzte 30 Tage)
Sam G.
Sam G. am 17 Feb. 2011
Below is the section of interest from a text file I'm trying to read.
.
.
.
3 0.68500 35.11850 -180.00000
3 0.68500 30.97850 0.00000
4 0.68500 99.97850 0.00000
4 -0.18500 99.97850 -180.00000
4 -0.18500 105.11850 0.00000
4 0.68500 105.11850 -180.00000
4 0.68500 99.97850 0.00000
5 79.57000 8.96350 0.00000
5 74.42000 8.96350 0.00000
5 74.42000 9.83350 -180.00000
5 79.57000 9.83350 0.00000
5 79.57000 8.96350 -180.00000
.END_BOARD_OUTLINE
.
.
.
The value I'm interested in is in the first column above the .END_BOARD_OUTLINE line, 5, in this case. Initially, I search through the file to find .END_BOARD_OUTLINE. I would then like to read the previous line to get the 5, but I can't find a way to move the cursor (for lack of a better term) up a line so I can read it. Is there a way to do this or do I need a different strategy to find that number?
I'm using fopen to open the file and fgetl to read the lines.

Akzeptierte Antwort

Andrew Newell
Andrew Newell am 17 Feb. 2011
You could store the current and previous lines like this:
currline = fgetl(fid);
while ischar(currline) && ~strcmp(currline,'.END_BOARD_OUTLINE')
prevline = currline;
currline = fgetl(fid);
end
Then prevline contains the line you want.
  2 Kommentare
Sam G.
Sam G. am 17 Feb. 2011
Yeah, that should work. Thanks! So there's no |goto| commands or anything like that?
Andrew Newell
Andrew Newell am 17 Feb. 2011
There is *fseek*, but you have to know how many bytes to go back.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

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