Fprintf works in one function but not in the other .

Discussion in 'C Programming' started by Smegly, May 25, 2004.

  1. Smegly

    Smegly Guest

    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
     
    Smegly, May 25, 2004
    #1
    1. Advertising

  2. Smegly wrote:
    >
    > 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.

    --
    Russell Hanneken

    Remove the 'g' from my address to send me mail.
     
    Russell Hanneken, May 25, 2004
    #2
    1. Advertising

  3. "Smegly" <> wrote in message news:40b30589$...
    > 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'.
    [..]
     
    Vijay Kumar R Zanvar, May 25, 2004
    #3
  4. Vijay Kumar R Zanvar wrote:
    > "Smegly" <> wrote in message news:40b30589$...
    >>
    >>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'.


    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.

    --
    Russell Hanneken

    Remove the 'g' from my address to send me mail.
     
    Russell Hanneken, May 25, 2004
    #4
  5. 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
    "Smegly" <> wrote in message
    news:40b30589$...
    > 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
    >
    >
    >
     
    Matthew Jakeman, May 25, 2004
    #5
  6. Smegly

    Smegly Guest

    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


    "Matthew Jakeman" <> wrote in message
    news:c8v3co$nib$...
    > 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
    > "Smegly" <> wrote in message
    > news:40b30589$...
    > > 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
    > >
    > >
    > >

    >
    >
     
    Smegly, May 25, 2004
    #6
  7. Smegly

    Karthik Guest

    Smegly wrote:

    > 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.

    --
    Karthik.
    Humans please 'removeme_' for my real email.
     
    Karthik, May 25, 2004
    #7
  8. >
    > 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.
     
    Mario Charest, May 26, 2004
    #8
  9. Smegly

    Dan Pop Guest

    In <A2ytc.46176$> "Mario Charest" <postmaster@127.0.0.1> writes:

    >>
    >> 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.


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

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, May 28, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrei Pociu
    Replies:
    6
    Views:
    1,521
    Alan Silver
    Mar 14, 2005
  2. Mark
    Replies:
    9
    Views:
    716
    Steve W. Jackson
    Jan 16, 2004
  3. F. GEIGER
    Replies:
    3
    Views:
    775
    F. GEIGER
    Aug 6, 2004
  4. Gary
    Replies:
    6
    Views:
    494
    Richard
    Aug 6, 2006
  5. lawrence
    Replies:
    3
    Views:
    195
Loading...

Share This Page