0

Thanks for any help! First off, i have a text file, text.txt, saved in my matlab folder. I would like to read this file, and have the text displayed when i run the program. I also only want to display up to a certain point in the text with the word Goal.

fid = fopen('text.txt', 'r+');

fprintf(fid, '%s');

fclose(fid);

This is my first part, without displaying up to the certain point of text. What i think i did was open up the file for reading, then print the document, then close the file. I don't get any errors or anything, but also don't see the document getting reprinted. Any ideas on how to get it printed would help!

4

2 回答 2

0

这里fprintf(fid, '%s')的意思是把东西打印到那个文件。

利用

fscanf(fid, '%s', s)
print(s)

从文件中读取。

利用

s = 'abc'
fprintf(fid, '%s', s)

打印到该文件。如果您需要打印到该文件,您应该使用

fid = fopen('text.txt', 'rw+')
于 2012-08-31T02:08:04.653 回答
0
fid=fopen('c:\text.txt','r');
s=textscan(fid,'%s','delimiter','\n');%u get array of lines, drop the delimiter part to get an array of words
ss=[s{1}];
fclose(fid);

%print out line by line on the screen, or do what ever you want with array of strings ss

i=0;<br>
j=length(ss);
while i < j;%or insert string compare to your 'Goal' word here to stop output if u have array of words in ss like while ~strcmp(ss(i+1),'Goal') or use strfind function to find your key word in lines of text<br>
  i=i+1;
  disp(ss(i));
end

希望能帮助到你

于 2012-08-31T03:18:14.003 回答