problem in fprintf
Show older comments
hi,
In the following code ,at first I opened file for reading in order to replace '|' with ','.
Then I opened another file for writing the the above data. Each time the number of ',' equal 23 , the writing of data in file will be in new line.
But, the resulting file just one line .
%%%%%%%%%%%%%%%%%%%%%
the code:
f=fopen('u2.txt','r');%%%%%%open the original data file
b=fscanf(f,'%s');
for i=1:length(b)
if b(i)=='|'
b(i)=',';
end
end
k=0;
f1=fopen('u4.txt','w+');
for j=1:length(b)
fprintf(f1,'%s',b(j));
if b(j)==','
k=k+1;
end
if k==23
k=0;
fprintf(f1,'%s','\n');
end
end
%%%%%%%%%%%%%%%%%
thanks
Answers (1)
Walter Roberson
on 13 Sep 2011
0 votes
Either use just about any other editor than the one you are using to view the file, or else use 'wt+' instead of 'w+' when you open the output file.
Note: you should really only use 'w+' or 'wt+' if you are going to be using fseek() to move around in the output file while it is still open. If all you need to do is write the file, then please use 'w' for binary files and 'wt' for text files.
Categories
Find more on Low-Level File I/O in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!