471,850 Members | 861 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,850 software developers and data experts.

fprintf

Hello all,

I want to write the following line:

mtext(1, at=Graph, text=sprintf('%0.2f',MP$LR), line=1)

in a file (for example:Hist.R) in a function as follow:

void Graph()
{
FILE *RH=fopen("Hist.R","w");
..
..
..
fprintf(RH,"mtext(1, at=Graph, text=sprintf('%0.2f',MP$LR),
line=1)\n");
..
..
..
fclose(RH);

When I run the program, what program writes in the 'Hist.R' file
is:

mtext(1, at=Graph, text=sprintf('0.00',MP$LR), line=1)

it is '0.00' instead of '%0.2f'. Can anybody help me to solve
this problem?

Thanks,
Mohsen

Oct 9 '06 #1
2 4647
Mohsen wrote:
Hello all,

I want to write the following line:

mtext(1, at=Graph, text=sprintf('%0.2f',MP$LR), line=1)

in a file (for example:Hist.R) in a function as follow:

void Graph()
{
FILE *RH=fopen("Hist.R","w");
.
.
.
fprintf(RH,"mtext(1, at=Graph, text=sprintf('%0.2f',MP$LR),
line=1)\n");
.
.
.
fclose(RH);

When I run the program, what program writes in the 'Hist.R' file
is:

mtext(1, at=Graph, text=sprintf('0.00',MP$LR), line=1)

it is '0.00' instead of '%0.2f'. Can anybody help me to solve
this problem?
1) You need to double the % character in your string above, so that it
is interpreted by fprintf correctly.

2) Consider using iostreams instead of stdio.

Nate
Oct 9 '06 #2
Mohsen wrote:
.
fprintf(RH,"mtext(1, at=Graph, text=sprintf('%0.2f',MP$LR),
line=1)\n");
.

When I run the program, what program writes in the 'Hist.R' file
is:

mtext(1, at=Graph, text=sprintf('0.00',MP$LR), line=1)

it is '0.00' instead of '%0.2f'. Can anybody help me to solve
this problem?
fprintf(RH, "%s", "your stuff goes here");
or
fputs("your stuff goes here", RH);

What is currently happening is that fprintf treats your text as
a format string, so it does something with any '%' it sees.

Oct 10 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Andrew Fabbro | last post: by
6 posts views Thread by Magix | last post: by
4 posts views Thread by baumann | last post: by
4 posts views Thread by grimrob | last post: by
16 posts views Thread by Prayag Narula | last post: by
3 posts views Thread by vamsi | last post: by
NeoPa
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by
aboka
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.