fprintf makes unseen ' marks in an outputfile?
1 view (last 30 days)
Show older comments
I have written a sliver of code to write an input file for an old 32 bit application. The offending code is below:
f1='GLU5D %1.4f\rGLU4Q %1.4f\rGLU4D45 %1.4f\rGLU4D34 %1.4f\r';
fprintf(file, f1,C5D,C4Q,C4D45,C4D34);
The output when you open it in wordpad looks like this:
GLU5D 0.5537
GLU4Q 0.3417
GLU4D45 0.3570
GLU4D34 0.1599
But when I submit the file to my .exe for processing, I get the following error:
GLU4Q' file test.are: expected a number for GLU5D, but found '0.5537
When I go into the wordpad file and delete one of the spaces before the 0.5537, it works fine. And it is not just the first space before the 0.5537, apparently any of the spaces will work. So have I picked up an invisible ' somewhere?
Help!
1 Comment
Image Analyst
on 2 Jan 2013
I see no reason why an extra apostrophe is being inserted. Did you open the file as text or binary? Let's see your fopen statement.
And 1.4f is not standard. The first number is the total field width, so it should be at least 4 (the number to the right of the decimal place) plus 1 (for the decimal place itself). But unless you have a bunch of spaces to the left that you want to pad it with, you don't even need the first number, just use %.4f - that's what I do.
Accepted Answer
Walter Roberson
on 2 Jan 2013
Change
f1='GLU5D %1.4f\rGLU4Q %1.4f\rGLU4D45 %1.4f\rGLU4D34 %1.4f\r';
to
f1='GLU5D %1.4f\nGLU4Q %1.4f\nGLU4D45 %1.4f\nGLU4D34 %1.4f\n';
and when you fopen() the file, instead of using 'w' use 'wt'
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!