find a string in a text file and to check particular character

4 次查看(过去 30 天)
I have following texts in a file:
...
RECORD_A
BE_FUNC
1; "scalar"; 1.0; 0.0; 2; 0
ACCEPT;ACCEPT
9; 1; 0; 0; 0
ACCEPT;ACCEPT
-5; 1;5.1; 0; 0
EN_FUNC
GIBT_ER
...
In this file 'RECORD_A' , 'BE_FUNC', 'EN_FUNC' , 'GIBT_ER' are unique strings . I am copying each line from a file and printing to another file.(let line number of BE_FUNC is 'n'). Now, I want to check if the line is equal to ' BE_FUNC ' ,then i want to check what is there in the 5th position (delimiter ';') in the next line (that is line n+1, here it is : 1; "scalar"; 1.0; 0.0; 2; 0 and the values are varying depends on the some other inputs ) and at the same time i have to print BE_FUNC to the other file as told already. And if it is '2' i have to check line n+3 and n+5 line (but need to print n,n+1,n+2,n+3,n+4,n+5 etc. as it is ) if it is '3' i have to check n+3,n+5,n+7 line (but need to print n,n+1,n+2,n+3,n+4,n+5,n+6,n+7 etc. as it is ).
main problem is : i have to complete one loop to print 'BE_FUNC' before going to n+1 line.
Here is my uncompleted program:
fid_old= fopen('dummyfile.txt','r+'); %read from temp file
fid_new= fopen ('newfile.txt','wt'); % creating a new file
while 1
tline = fgetl(fid_old);
if ~ischar(tline)
break
end
if strfind(tline,'BE_FUNC')==1
disp('Here your help needed, Thank you')
end
fprintf(fid_new,tline);
fprintf(fid_new,'\n');
end
fclose(fid_old);
fclose(fid_new);
And Everything in a single while loop is pretty much expected.

回答(1 个)

Sreeram Mohan
Sreeram Mohan 2014-9-23
Hi Vipin,
Have you tried using strtok ? This might be more useful in the case you are dealing with.
Thanks,
Sreeram Mohan
  1 个评论
Vipin Mohan
Vipin Mohan 2014-9-23
编辑:Vipin Mohan 2014-9-23
hi, the problem is i have to print it on the same time !!! I have to check and print 'BE_FUNC', and if it yes, i have to go to next line to find out the character '2' in the next line..and strtok can be used in the n+1 line only... before that i have to print!!!!!! i have to complete one loop to print 'BE_FUNC' before going to n+1 line.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by