DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Assertion Failure? (http://www.daniweb.com/forums/thread74631.html)

shmay Apr 8th, 2007 3:29 pm
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?

vijayan121 Apr 8th, 2007 3:39 pm
Re: Assertion Failure?
 
close the file: fclose

shmay Apr 8th, 2007 3:47 pm
Re: Assertion Failure?
 
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.

~s.o.s~ Apr 8th, 2007 4:01 pm
Re: Assertion Failure?
 
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.

vijayan121 Apr 8th, 2007 4:05 pm
Re: Assertion Failure?
 
since the debud assertion that fails is:
Quote:

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.

shmay Apr 8th, 2007 4:07 pm
Re: Assertion Failure?
 
Quote:

Originally Posted by ~s.o.s~ (Post 340950)
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.

vijayan121 Apr 8th, 2007 4:09 pm
Re: Assertion Failure?
 
Quote:

Originally Posted by ~s.o.s~ (Post 340950)
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.

~s.o.s~ Apr 8th, 2007 4:09 pm
Re: Assertion Failure?
 
Programming brings to worst out of us... ;)

You are welcome.

vijayan121 Apr 8th, 2007 4:20 pm
Re: Assertion Failure?
 
Quote:

Originally Posted by shmay (Post 340935)
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.


All times are GMT -4. The time now is 1:57 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC