Home > Archive > Matlab > June 2007 > fscanf returns 50% empty lines
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
fscanf returns 50% empty lines
|
|
|
| hi,
i like to read a file that has a decimal-"," and write a file where
the "," are replaced by ".". i want to use fscanf, Matlab 7.3,
windows2000:
fid = fopen ('D:\testarea\Rastervermessung
Bearbeitet\Kennfeld_1500.csv');
A = fscanf(fid,'%c');
fclose (fid);
pos = strfind (A, ',');
if (pos ~= 0)
curLine(pos) = '.';
end
newFile = fopen ('test.txt', 'wt');
fprintf(newFile, '%s', A);
fclose (fid);
however, the fscanf function returns an empty line in every 2nd line.
if i use '%f' format it deletes every lineseperator though. Any ideas
what the problem is? Thanx in advance, Jan
| |
| Peter Boettcher 2007-06-11, 7:13 pm |
| jan <kartoffelmensch@web.de> writes:
> hi,
> i like to read a file that has a decimal-"," and write a file where
> the "," are replaced by ".". i want to use fscanf, Matlab 7.3,
> windows2000:
>
> fid = fopen ('D:\testarea\Rastervermessung
> Bearbeitet\Kennfeld_1500.csv');
> A = fscanf(fid,'%c');
> fclose (fid);
>
> pos = strfind (A, ',');
> if (pos ~= 0)
> curLine(pos) = '.';
> end
>
> newFile = fopen ('test.txt', 'wt');
> fprintf(newFile, '%s', A);
> fclose (fid);
>
> however, the fscanf function returns an empty line in every 2nd line.
> if i use '%f' format it deletes every lineseperator though. Any ideas
> what the problem is? Thanx in advance, Jan
You are opening the file for writing with the 'text' flag ('wt'), but
you do not do the same for reading. That means you get an explicit
carriage return AND linefeed in your A array. Then, when you print it
back out in text mode, Windows helpfully expands your LF to be
CR + LF, leaving you with an extra CR.
In short, open for reading with 'rt'
-Peter
| |
|
| And i spent like 2 hours searching for the mistake :) Thanks alot!
Peter Boettcher wrote:
>
>
> jan <kartoffelmensch@web.de> writes:
>
> where
> line.
> ideas
>
> You are opening the file for writing with the 'text' flag ('wt'),
> but
> you do not do the same for reading. That means you get an explicit
> carriage return AND linefeed in your A array. Then, when you print
> it
> back out in text mode, Windows helpfully expands your LF to be
> CR + LF, leaving you with an extra CR.
>
> In short, open for reading with 'rt'
>
> -Peter
>
|
|
|
|
|