944,144 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 23765
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 8th, 2007
0

Assertion Failure?

Expand Post »
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.


C++ Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shmay is offline Offline
55 posts
since Jul 2005
Apr 8th, 2007
0

Re: Assertion Failure?

close the file: fclose
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Apr 8th, 2007
0

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shmay is offline Offline
55 posts
since Jul 2005
Apr 8th, 2007
0

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.
Last edited by ~s.o.s~; Apr 8th, 2007 at 4:01 pm.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Apr 8th, 2007
0

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.
Last edited by vijayan121; Apr 8th, 2007 at 4:06 pm. Reason: formatting
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Apr 8th, 2007
0

Re: Assertion Failure?

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shmay is offline Offline
55 posts
since Jul 2005
Apr 8th, 2007
0

Re: Assertion Failure?

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
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
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Apr 8th, 2007
0

Re: Assertion Failure?

Programming brings to worst out of us...

You are welcome.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Apr 8th, 2007
0

Re: Assertion Failure?

Click to Expand / Collapse  Quote originally posted by shmay ...
Why is it giving me this?
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Feb 16th, 2011
-2

This code shows assertion failure after setting and displaying the matrix values

//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;
}
Reputation Points: 9
Solved Threads: 0
Newbie Poster
viki_chennai is offline Offline
2 posts
since Feb 2011
Message:
Previous Thread in C++ Forum Timeline: Policy vs polymorphic timing
Next Thread in C++ Forum Timeline: Variable Types





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC