Assertion Failure?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2005
Posts: 55
Reputation: shmay is an unknown quantity at this point 
Solved Threads: 0
shmay shmay is offline Offline
Junior Poster in Training

Assertion Failure?

 
0
  #1
Apr 8th, 2007
I'm trying to read from a text file. Pretty simple, right? I guess not. It gives me this error message:

Debug Assertion Failed!

Program: F:\Debug\lab1431.exe
File: fscanf.c

Expression: stream != NULL

For information on how your program can cause assertion failure, see the Visual C++ documentation on asserts.


  1. #include <stdio.h>
  2. #define FILENAME "F:/Debug/lab14data.txt"
  3.  
  4. int main(void)
  5. {
  6. int num_data_pts = 0, fday, day = 1;
  7. double ffahr, max, min;
  8. FILE *temp_each_day;
  9.  
  10. temp_each_day = fopen(FILENAME, "r");
  11. if (temp_each_day = NULL)
  12. printf("Error opening input file. \n");
  13. else
  14. {
  15. while (fscanf(temp_each_day,"%i %lf", &fday, &ffahr) == 2)
  16. {
  17. num_data_pts++;
  18. day++;
  19.  
  20. printf(" Day Fahrenheit Celsius");
  21. printf("%4i %7lf %7lf", fday, ffahr, (ffahr + 32) / 1.8);
  22.  
  23. if (num_data_pts == 1)
  24. max = min = ffahr;
  25. else if (ffahr > max)
  26. max = ffahr;
  27. else if (ffahr < min)
  28. min = ffahr;
  29. }
  30. }
  31.  
  32. return 0;
  33. }
(program is unfinished, but still should work).

Why is it giving me this?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Assertion Failure?

 
0
  #2
Apr 8th, 2007
close the file: fclose
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 55
Reputation: shmay is an unknown quantity at this point 
Solved Threads: 0
shmay shmay is offline Offline
Junior Poster in Training

Re: Assertion Failure?

 
0
  #3
Apr 8th, 2007
I just did that and it still comes up. I think it has to do with the opening of the file because if I remove the fscanf line, the error message doesn't come up.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Assertion Failure?

 
0
  #4
Apr 8th, 2007
if (temp_each_day = NULL)
        printf("Error opening input file. \n");
The = in the above code should be replaced by == if what you want to do is comparision.
Last edited by ~s.o.s~; Apr 8th, 2007 at 4:01 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Assertion Failure?

 
0
  #5
Apr 8th, 2007
since the debud assertion that fails is:
Debug Assertion Failed!
Program: F:\Debug\lab1431.exe
File: fscanf.c
Expression: stream != NULL
the error is that at a place where the
stream was expected to be NULL, it is not NULL. stream (in the FILE structure)
would be NULL after the file has been closed.
remember that fscanf can corrupt memory if your format specifiers are incorrect.
Last edited by vijayan121; Apr 8th, 2007 at 4:06 pm. Reason: formatting
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 55
Reputation: shmay is an unknown quantity at this point 
Solved Threads: 0
shmay shmay is offline Offline
Junior Poster in Training

Re: Assertion Failure?

 
0
  #6
Apr 8th, 2007
Originally Posted by ~s.o.s~ View Post
if (temp_each_day = NULL)
        printf("Error opening input file. \n");
The = in the above code should be replaced by == if what you want to do is comparision.
I am an idiot. THANK YOU.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Assertion Failure?

 
0
  #7
Apr 8th, 2007
Originally Posted by ~s.o.s~ View Post
if (temp_each_day = NULL)
        printf("Error opening input file. \n");
The = in the above code should be replaced by == if what you want to do is comparision.
that is clearly the error. once you assign NULL to temp_each_day,
a. the if block will not execute.
b. fclose(NULL) will assert and an assertion wil fail on exit if fclose is not called.
Last edited by vijayan121; Apr 8th, 2007 at 4:11 pm. Reason: format
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Assertion Failure?

 
0
  #8
Apr 8th, 2007
Programming brings to worst out of us...

You are welcome.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Assertion Failure?

 
0
  #9
Apr 8th, 2007
Originally Posted by shmay View Post
Why is it giving me this?
  1. if (temp_each_day = NULL)
the compiler would have given a warning for this.
tip: set the warning level of the compiler to the highest available, and pay attention to the warnings.
you would save yourself a great deal of effort and time.
Last edited by vijayan121; Apr 8th, 2007 at 4:21 pm. Reason: format
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC