CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2004
    Posts
    38

    fprintf question

    im curious as to what conditions would make fprintf not work

    i have a valid file ptr, i have opened the file with fopen and "w"
    and the code before and after my fprintf statement works
    it just wont write to the file (the file remains empty)

    the file in question is just a command line argument that gets open so the permissions are fine as well

    any thoughts

  2. #2
    Join Date
    Jun 2003
    Location
    Gjøvik, Norway
    Posts
    204
    Some code would be nice... At the very least your fprintf line and the variables it uses.

  3. #3
    Join Date
    Apr 2004
    Location
    Wisconsin
    Posts
    10
    Without code, I have to take a guess, but guess I will:

    1) Is fprintf returning an error code?

    2) Is it possible that you still have the file open and the application paused (or blocked or somehow waiting) at the point at which you are manually checking through another window to see what contents it has??

    Like the Win32 counterparts, the C Runtime stream routines implement file-buffering by default, so if you REALLY need to see the output immediately after an 'fprintf', you need to make an "fflush(fp)" call to force the data out to disk.

    In a performance-oriented application, I would NOT recommend calling fflush() after every fprintf/fwrite-type call.

    The other approach is using _setvbuf() to turn off buffering for the stream, but I would only recommend doing this if you *really* need to be able to monitor the output as it comes out.

    Cheers!

  4. #4
    Join Date
    Feb 2004
    Posts
    38
    i do not mind posting code but i have a lot of it

    basically let me give you a quick sumation of what i am doing


    i have a server program that fork and exec's a client program
    using FIFO's i read a command from a file in the client
    i pass the info to the server

    the server process's the command and sends a response

    this response is what i cannot seem to write to a logfile

    i use for example

    fprintf(fptr, "just print darn you %s\n", response);

    and nothing happens

    if i put say a printf b4 and after the fprintf both of those print to the screen

    but the file remains empty

    what you said about waiting does make sense but...
    why would it need to wait if the printf b4 and after it works just fine

  5. #5
    Join Date
    Feb 2004
    Posts
    38
    this is on unix using c btw

    i am just perplexed as to why it doesnt work

  6. #6
    Join Date
    Apr 2004
    Location
    Wisconsin
    Posts
    10
    Un*x also buffers the stream I/O routines, so my comments above still apply.

    The reason your printf's work is they go to the console whereas your fprintf goes to a completely different stream. Throw in an fflush(fptr) statement and see if it appears.

  7. #7
    Join Date
    Feb 2004
    Posts
    38
    leeND you are a genious

    amidst my chaos i would of never considered to just flush
    but yes it works like a champ

    thank you

    rock

    if can figure out how to rate you i would and will

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured