Assertion Failure?
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.
#include <stdio.h>
#define FILENAME "F:/Debug/lab14data.txt"
int main(void)
{
int num_data_pts = 0, fday, day = 1;
double ffahr, max, min;
FILE *temp_each_day;
temp_each_day = fopen(FILENAME, "r");
if (temp_each_day = NULL)
printf("Error opening input file. \n");
else
{
while (fscanf(temp_each_day,"%i %lf", &fday, &ffahr) == 2)
{
num_data_pts++;
day++;
printf(" Day Fahrenheit Celsius");
printf("%4i %7lf %7lf", fday, ffahr, (ffahr + 32) / 1.8);
if (num_data_pts == 1)
max = min = ffahr;
else if (ffahr > max)
max = ffahr;
else if (ffahr < min)
min = ffahr;
}
}
return 0;
}
(program is unfinished, but still should work).
Why is it giving me this?
shmay
Junior Poster in Training
55 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
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.
shmay
Junior Poster in Training
55 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
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.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
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.
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
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.
shmay
Junior Poster in Training
55 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
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.
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
Programming brings to worst out of us... ;)
You are welcome.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
Why is it giving me this?
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.
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287