Fprintf works in one function but not in the other .

S

Smegly

Hi,

I'm confused about a situation i have ..

fprintf works in one function, but not in the other . .

This is my part of my code for the two functions ..


void customer(FILE *fin, FILE *fout, int msqid, message_buf sbuf, size_t
buf_length, int timer)
----------------------------------------------------------------------------
-----


//printf("%s\n", buff);
time(&now);
fprintf(fout,"--------------------------------------------------\n");
fprintf(fout,"Customer %s: %s\rArrival time: %s\r", custRef,
custAction, ctime(&now));

//fprintf(fout,"--------------------------------------------------\n");

----------------------------------------------------------------------------
------

this fprintf works. . .

now this is part of the other function teller()

void teller(FILE *fptr1, message_buf rbuf, int msqid, int i, int Td, int Tw)
----------------------------------------------------------------------------
-----

printf("Customer statistics for Teller %d\n", getpid());
printf("Servicing customer %s - Action required %s\n", custNum,
custAct);
fprintf(fout, "Customer statistics for Teller %d\n",getpid());

----------------------------------------------------------------------------
----

this fprintf does not work . .


Does anyone know why this would be the case ?

Matt
 
R

Russell Hanneken

Smegly said:
fprintf(fout, "Customer statistics for Teller %d\n",getpid());

this fprintf does not work . .

In what sense does it "not work?" It doesn't compile? It causes your
program to crash? It prints something you didn't expect?
Does anyone know why this would be the case ?

I can't compile the teller function, because message_buf is an
unrecognized type, and custNum, custAct, and fout haven't been declared
in the scope of the teller function. Is this the same problem you're
experiencing? If not, you need to take out whatever isn't necessary,
and add whatever is necessary, to show us your problem.
 
V

Vijay Kumar R Zanvar

Smegly said:
Hi,

I'm confused about a situation i have ..

fprintf works in one function, but not in the other . .

This is my part of my code for the two functions ..


void customer(FILE *fin, FILE *fout, int msqid, message_buf sbuf, size_t
buf_length, int timer)
----------------------------------------------------------------------------
-----


//printf("%s\n", buff);
time(&now);
fprintf(fout,"--------------------------------------------------\n");
fprintf(fout,"Customer %s: %s\rArrival time: %s\r", custRef,
custAction, ctime(&now));

I think the carriage return character, '\r', is your culprit.
Try changing it to the newline character, '\n'.
[..]
 
R

Russell Hanneken

I think the carriage return character, '\r', is your culprit.
Try changing it to the newline character, '\n'.

You snipped the line where he says "this fprintf works." He says the
problem is with the second fprintf statement in his post, which is in
another function.
 
M

Matthew Jakeman

In the second function you do fprintf(fout, ...) ;
Where is fout defined, in the first function it is clearly defined but I
can't see it defined in the second one?
Good Luck!
The Fat Man
 
S

Smegly

sorry i corrected the code in the editor ..

The void teller(FILE *fptr1, message_buf rbuf, int msqid, int i, int Td, int
Tw)

is actually FILE *fout, etc ... )

the statement does not add any thing to the simulation_log file.

Matt
 
K

Karthik

Smegly said:
sorry i corrected the code in the editor ..

The void teller(FILE *fptr1, message_buf rbuf, int msqid, int i, int Td, int
Tw)

is actually FILE *fout, etc ... )

the statement does not add any thing to the simulation_log file.

Matt

Seems like you had a global variable by name - fout .
See, now you have got a valid reason why you should try to avoid global
variables as far as possible.
 
M

Mario Charest

The void teller(FILE *fptr1, message_buf rbuf, int msqid, int i, int Td, int
Tw)

is actually FILE *fout, etc ... )

the statement does not add any thing to the simulation_log file.

Matt
Test the return value of fprintf (yes you can do that), I expect it to
return an error then check errno. Most likely the file handle fout is
invalid. Also make sure getpid() has its prototype included.
 
D

Dan Pop

In said:
Test the return value of fprintf (yes you can do that), I expect it to
return an error then check errno. Most likely the file handle fout is
invalid.

Then, the fprintf call invokes undefined behaviour and all the bets are
off.

Dan
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top