Thread: Checking for errors on output streams

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    8

    Checking for errors on output streams

    Code:
    void write_deletion(D_RECORD *record, FILE *outfile)
    {
    	fputc(record->type,outfile);
    	fputc(record->code[0],outfile);
    	fputc(record->code[1],outfile);
    	fputc(record->code[2],outfile);
    	fputc(record->code[3],outfile);
    	fputc(record->code[4],outfile);
    	fputc('\n',outfile);
    }
    I want to check if there has been an error writing to the outfile stream, could I just add an if( ferror(outfile) ){...}, after the call to the above function, in the calling function? Checking for EOF from each of the fputc calls will look a little messy...
    I need to take into account that the HD may have filed up etc, and alert the user that there was an error.
    Also, if there is an error, what is a good way to print it?
    perror(FILENAME_OF_OUTFILE); is ok?

    Thanks in advance.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Check the return code of the function doing the writing for the best results, imho.

    Lookup fwrite(), that might do you a better job than multiple calls the fputc().

    >>perror(FILENAME_OF_OUTFILE); is ok?
    Yes, that's OK.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Problem using sscanf fgets and overflow checking
    By jou00jou in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 06:42 AM
  4. Trying to store system(command) Output
    By punxworm in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2005, 06:46 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM