954,124 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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
 

close the file: fclose

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
Administrator
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
Administrator
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
 

//C++ class to get matrix elements as input from user
class matrix_input
{
private:
int nof_row,nof_column;
int row_count,column_count;
int nof_matrix;
int **address;
public:
matrix_input();
int get_no_row();
int get_no_col();
int mat_elem_memalloc();
int getmatrix();
void show_matrix();
~matrix_input();
};
#include "matrix_input.h"
#include
using namespace std;
//
matrix_input::matrix_input()
{
nof_row=0;
nof_column=0;
row_count=0;
column_count=0;
}
//
int matrix_input::get_no_row()
{
int no_row;

cout<<"Enter the no of rows in matrix\n";
cin>>no_row;
nof_row=no_row;

return nof_row; //the last overwrited value

}
//
int matrix_input::get_no_col()
{
int no_col;

cout<<"Enter the no of columns in matrix\n";
cin>>no_col;
nof_column=no_col;

return nof_column; //the last overwrited value
}
//
int matrix_input::mat_elem_memalloc()
{
int **ptr,i;

ptr=new int*[nof_row];
for(i=0;i>address[row_count][column_count];
}
}
return **address;

}
//
void matrix_input::show_matrix()
{
cout<<"\n";

for(row_count=0;row_count
using namespace std;
int main()
{
matrix_input A,B;
A.get_no_row();
A.get_no_col();
A.getmatrix();
A.show_matrix();
A.~matrix_input();


return 0;
}

viki_chennai
Newbie Poster
2 posts since Feb 2011
Reputation Points: 9
Solved Threads: 0
 

#include
#include
#include
#include
#define NUM 0
#define ID 1
#define SEMICOLON 2
#define OPEN_BRACKET 3
#define CLOSE_BRACKET 4
#define forward 5
#define pen 6
#define on 7
#define off 8
#define repeat 9
#define color 10
#define turn 11
#define right 12
#define left 13
#define SLASH 14
#define POWER 15
#define MULTIPLY 16
#define Backward 17
#define SUBTRACTION 18
#define ADDITION 19


using namespace std;


ifstream infile;

int getvalue()
{
char ch;
infile.get(ch);
switch (ch)
{
case ';' : return SEMICOLON;
break;
case '{' : return OPEN_BRACKET;
break;
case '}' : return CLOSE_BRACKET;
break;
case '/' : return SLASH;
break;
case '^' : return POWER;
break;
case '*' : return MULTIPLY;
break;
case '-' : return SUBTRACTION;
break;
case '+' : return ADDITION;
break;
}
if (ch >= '0' && ch <= '9')
{
do
{
infile.get(ch);
}while (isdigit(ch));
return NUM;
}
else
if (isalpha(ch))
{
do
{
infile.get(ch);
}while (isalpha(ch));
return ID;
}

}


int main()
{

infile.open("input.slg");
int c;

while(!infile.eof())
{
c = getvalue();

cout << c << endl;

}
infile.close();
return 0;
}

I have an assertion failure error in this program. Please help solving it.

Rosa_Indica
Newbie Poster
1 post since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You