Checking for errors on output streams

This is a discussion on Checking for errors on output streams within the C Programming forums, part of the General Programming Boards category; 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 ...

  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, 10: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, 07:46 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21