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

18 次查看(过去 30 天)
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.

采纳的回答

Andrew Newell
Andrew Newell 2011-2-17
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 个评论
Sam G.
Sam G. 2011-2-17
Yeah, that should work. Thanks! So there's no |goto| commands or anything like that?
Andrew Newell
Andrew Newell 2011-2-17
There is *fseek*, but you have to know how many bytes to go back.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Low-Level File I/O 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by