필터 지우기
필터 지우기

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 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