Clear Filters
Clear Filters

fprintf makes unseen ' marks in an outputfile?

2 views (last 30 days)
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
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.

Sign in to comment.

Accepted Answer

Walter Roberson
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'
  1 Comment
Matthew
Matthew on 2 Jan 2013
I was already using 'wt+' so that was not it, but for the life of me, what is the difference between carriage return and newline? Switching the \n for the \r definitely fixed the problem. THANKS

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!