fprintf makes unseen ' marks in an outputfile?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2013-1-2
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.
采纳的回答
Walter Roberson
2013-1-2
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'
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Low-Level File I/O 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!