fprintf makes unseen ' marks in an outputfile?

3 ビュー (過去 30 日間)
Matthew
Matthew 2013 年 1 月 2 日
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
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
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'
  1 件のコメント
Matthew
Matthew 2013 年 1 月 2 日
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

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by