fprintf

M

Mohsen

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
 
N

Nate Barney

Mohsen said:
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
 
O

Old Wolf

Mohsen said:
.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top