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?

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.

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.

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.

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.

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.

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.

//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 <iostream>
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<nof_row;i++)   
     {
        ptr[i]=new int[nof_column];
     }
     address=ptr;                               //* to * or ** to ** is possible
     return **address;                    //In return * or ** symbol should be added in pointers

  //dummy  return value
}
//
int matrix_input::getmatrix()
{

    mat_elem_memalloc();
    cout<<"Enter matrix elements\n";
        for(row_count=0;row_count<nof_row;row_count++)
        {
            for(column_count=0;column_count<nof_column;column_count++)
            {
                cin>>address[row_count][column_count];
            }
        }
        return **address;

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

        for(row_count=0;row_count<nof_row;row_count++)
        {
            for(column_count=0;column_count<nof_column;column_count++)
            {
                cout<<address[row_count][column_count]<<" ";
            }
            cout<<"\n";
        }

}
//
matrix_input::~matrix_input()
{
    int i;
    for(i=0;i<nof_row;i++)   
     {
        delete [] address[i];
     }
    delete [] address;
}
#include "matrix_input.h"
#include <iostream>
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;
}

Edited 3 Years Ago by Dani: Formatting fixed

Comments
Don't bump old threads for something like this
Why the Necro?
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#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.

Edited 3 Years Ago by Dani: Formatting fixed

hey when i start game called silkroad i get this massege in box title :
BSLib Debug Module

Debug Assertion Failed

Espression = 0
Line: 831
File : D:\vssod\Silkroad\Client\client\Client.cpp

CallStack:

(Press 'ok' to advance,Press 'cancel' to exit

This article has been dead for over six months. Start a new discussion instead.