views:

353

answers:

4

my problem is fprintf is only printing part of the expected output into the file.When i use printf the output is correctly printed on the output window, showing that the loop is correct but when i use it with fprintf the complete output is not printed.Only the initial part is printed.

PLease advise as to what might possibly be the problem???

thanks in advance...

+4  A: 

I bet that you've not flushed/closed your file.

akappa
+2  A: 

Sound like you forgot to do fflush() or fclose().

Christoffer
A: 

you try to use fflush()

I downvoted this because the answer simply doesn't make sense, probably due to english not being your native language. You said "you try to use fflush" but he didn't try to use fflush. Perhaps you meant he _should_ try to use it? If so, that's already been suggested several times so there's not point is saying it again.
Bryan Oakley
+3  A: 

The problem is likely that you are not telling C to actually write the data to disk. This usually happens automatically when you close a file, and may happen automatically at other times (such as when internal buffers fill up).

It sounds like you are writing just a few bytes and then checking the file to see what happened. If so, your program may be holding those bytes in an internal buffer before actually writing to disk. It does this to improve performance in the general case -- you don't normally want a disk access for each and every single print statement.

One solution, as other answers suggest, is to call fflush. This will "flush" all of the buffered data to disk. There are other solutions such as to turn off buffering, but calling fflush is the best first step since you are new to programming.

For more information, here's a link to a wiki book about file I/O with C. You can jump straight to the section on fflush, though you might want to read the introductory paragraphs to gain a little more insight.

Bryan Oakley