Thread: Fatal error in fprintf

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    108

    Fatal error in fprintf

    Hi, im trying to write a bunch of strings into a text file which are received through the com port.

    every so often though the program gives a fatal error and i get a dialog box which go's something like

    fatal error....

    ...

    str != NULL
    it points to line 56 of fprintf.c and says that an "assertion failure" was caused.

    i think it might be because its trying to write a string when it hasnt received anything.

    im using the code below to write my strings into the file

    Code:
     	fprintf(fp,"%s", decoded);
    i have modified it so it doesnt continue when it cant write the string so my new code looks like

    Code:
     if((fprintf(fp, "\n") || fprintf( fp,"%s", decoded)) == EOF)
    	   {
    		   printf("No string/String error");
    	   }
    	   else{					
    	          fprintf(fp,"%s", decoded);
    	          }
    howver, its still giving me the same error after a few hours

    Could someone tell me what the error is and what i might be doing wrong?

    thanks

    VC++ , windows XP
    Last edited by shoobsie; 10-14-2005 at 03:27 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by shoobsie
    Code:
     if((fprintf(fp, "\n") || fprintf( fp,"%s", decoded)) == EOF)
    	   {
    		   printf("No string/String error");
    	   }
    	   else{					
    	          fprintf(fp,"%s", decoded);
    	          }
    This code is horrible for multiple reasons:
    1) In the if check, it writes out a newline. It also writes out decoded.
    2) It takes the retrun value of both of those, and if either one is true (non-zero) it compares it to EOF. Read: It compares TRUE to EOF.
    3) Then, it simply writes decoded again.
    4) "No string/String error" will never display.

    I suspect that if the rest of your code is in the same vein as this simple portion here, you'll be a while in tracking down your error.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    hi, thanks for the reply.

    what i thought i was doing there was comparing initially to see if the two strings had an error in them and if they did then the error message would be displayed.

    otherwise, it would write the two strings down.

    i see you are right since what you said is occuring, can you suggest a remedy for this section?

    thanks

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Check the return value of whatever is acquiring your strings for you. Typically these types of functions return something useful, such as the amount of data read or the like. Use that to see if your string contains something useful. In addition to that, consider using something like strlen to test the length of the string. If it's less than 1, or some other number you decide on for whatever reason, then treat it as a garbage string.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    Quote Originally Posted by quzah
    Check the return value of whatever is acquiring your strings for you. Typically these types of functions return something useful, such as the amount of data read or the like. Use that to see if your string contains something useful. In addition to that, consider using something like strlen to test the length of the string. If it's less than 1, or some other number you decide on for whatever reason, then treat it as a garbage string.


    Quzah.

    thanks alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 04:19 PM
  2. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 10:33 PM
  3. sprintf and fprintf segmentation error
    By kona1 in forum C Programming
    Replies: 5
    Last Post: 06-21-2005, 10:55 AM
  4. fprintf to stderr crash programs
    By jlai in forum Windows Programming
    Replies: 2
    Last Post: 04-12-2005, 08:51 AM
  5. fatal exception 0E
    By juhigarg in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2001, 04:40 AM