フィルターのクリア

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

27 ビュー (過去 30 日間)
Sam G.
Sam G. 2011 年 2 月 17 日
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 Center および File ExchangeLow-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