468,513 Members | 1,759 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

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 4496
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
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.