When I run this program, and the file can be found, it works fine. However when the file is not there I get the error:

Debug Assertion Failed!

Expression (str != NULL)

It seems to be a problem with the following loop as when its remove it works fine:

FILE *fp;
	fp=fopen("texts.txt","r");

	if (fp == NULL) 
		printf("Can't open file!\n");
	else
		printf("File opened!\n");

	fclose;
#include<stdio.h>
#include<string.h>

void main()
{
	/*declare and initialise variable*/
	char message[400];
	int i=0;
	char filename[30];

	/*prog info*/
	printf("This program will read in a file of your choice and print it out to the screen.\nCreated 30/11/10 v.10\n\n\n\n");


	/*Input of file name from keyboard
	printf("Please input the name of the file you wish to use inc .txt suffix\nTip: File should be stored in the same directory as the program file\n");
	scanf("%s",filename);*/

	FILE *fp;
	fp=fopen("texts.txt","r");

	if (fp == NULL) 
		printf("Can't open file!\n");
	else
		printf("File opened!\n");

	fclose;
	

	/*stores and prints the data from the string*/
	fgets (message , 400 , fp);
	printf("\nYour text for encryption:\n");
    puts (message);//prints message

}

Edited 6 Years Ago by frankchester: n/a

I originally didn't have that in, but put it in there to try and remedy this problem as I read on another topic and that was the issue...

I'll remove it, but there or not it still doesn't work?

I originally didn't have that in, but put it in there to try and remedy this problem as I read on another topic and that was the issue...

I'll remove it, but there or not it still doesn't work?

Hi Frank,

You did a small mistake by used a fclose without mentioning the file pointer. If you want to close any file you should use

fclose(file_pointer); or 
fcloseall();

to close the particular file.

The below code will works with no error.

#include<stdio.h>
#include<string.h>
 
void main()
{
	/*declare and initialise variable*/
	char message[400];
	int i=0;
	char filename[30];
 
	/*prog info*/
	printf("This program will read in a file of your choice and print it out to the screen.\nCreated 30/11/10 v.10\n\n\n\n");
 
 
	/*Input of file name from keyboard
	printf("Please input the name of the file you wish to use inc .txt suffix\nTip: File should be stored in the same directory as the program file\n");
	scanf("%s",filename);*/
 
	FILE *fp;
	fp=fopen("texts.txt","r");
 
	if (fp == NULL) 
		printf("Can't open file!\n");
	else
		printf("File opened!\n"); 
            
        //fclose; /* This is the mistake you did. fclose is used without mentioning the file pointer. */
        //fclose(fp); /*Even if you use like this to close the file. you can't read the data from the file after this line. You will get garbage value if you use like this.Since you are reading after this line using fgets*/
 
	/*stores and prints the data from the string*/
	fgets (message , 400 , fp);
	fclose(fp); //File should be closed after all file operation

	printf("\nYour text for encryption:\n");
    puts (message);//prints message
 
}

Edited 6 Years Ago by Perry31: n/a

Hi Frank,

You did a small mistake by used a fclose without mentioning the file pointer. If you want to close any file you should use

fclose(file_pointer); or 
fcloseall();

to close the particular file.

The below code will works with no error.

#include<stdio.h>
#include<string.h>
 
void main()
{
	/*declare and initialise variable*/
	char message[400];
	int i=0;
	char filename[30];
 
	/*prog info*/
	printf("This program will read in a file of your choice and print it out to the screen.\nCreated 30/11/10 v.10\n\n\n\n");
 
 
	/*Input of file name from keyboard
	printf("Please input the name of the file you wish to use inc .txt suffix\nTip: File should be stored in the same directory as the program file\n");
	scanf("%s",filename);*/
 
	FILE *fp;
	fp=fopen("texts.txt","r");
 
	if (fp == NULL) 
		printf("Can't open file!\n");
	else
		printf("File opened!\n"); 
            
        //fclose; /* This is the mistake you did. fclose is used without mentioning the file pointer. */
        //fclose(fp); /*Even if you use like this to close the file. you can't read the data from the file after this line. You will get garbage value if you use like this.Since you are reading after this line using fgets*/
 
	/*stores and prints the data from the string*/
	fgets (message , 400 , fp);
	fclose(fp); //File should be closed after all file operation

	printf("\nYour text for encryption:\n");
    puts (message);//prints message
 
}

Hi, I just ran this exact piece of code and I'm STILL getting the same error. :/

I should point out that if the file CAN be found, the program runs perfectly, however its when the filen does not exist that it crashes. I don't think its a problem with the fclose thats causing all this, hence why I thought it was something in the loop.

Edited 6 Years Ago by frankchester: n/a

Hi, I just ran this exact piece of code and I'm STILL getting the same error. :/

I should point out that if the file CAN be found, the program runs perfectly, however its when the filen does not exist that it crashes. I don't think its a problem with the fclose thats causing all this, hence why I thought it was something in the loop.

Hi Frank,

This is not related to loop. Its related to file handling since file not found we need to handle the file pointer. So we need to stop the file pointer.

Below is the code with fix.

#include<stdio.h>
#include<conio.h>
 
int main()
{
	
	char message[400];
	int i=0;

 	printf("This program will read in a file of your choice and print it out to the screen.\n\n");
 
	FILE *fp;
	fp=fopen("texts.txt","r");
 
	if (fp == NULL) 
	{
		printf("Can't open file!\n");
		getch();
		return 0;
	}
	else
		printf("File opened!\n");
	
	
	fgets (message , 400 , fp);
	fclose(fp);

	printf("\nYour text for encryption:\n");
    puts (message);
	getch();
	return 1;
}

Hi Frank,

This is not related to loop. Its related to file handling since file not found we need to handle the file pointer. So we need to stop the file pointer.

Below is the code with fix.

#include<stdio.h>
#include<conio.h>
 
int main()
{
	
	char message[400];
	int i=0;

 	printf("This program will read in a file of your choice and print it out to the screen.\n\n");
 
	FILE *fp;
	fp=fopen("texts.txt","r");
 
	if (fp == NULL) 
	{
		printf("Can't open file!\n");
		getch();
		return 0;
	}
	else
		printf("File opened!\n");
	
	
	fgets (message , 400 , fp);
	fclose(fp);

	printf("\nYour text for encryption:\n");
    puts (message);
	getch();
	return 1;
}

Hi, thank you, this works!

Would you be able to tell me what the getch did and how the extras have fixed this, I'm studying so would like to make sure I know how things work!

Hi, thank you, this works!

Would you be able to tell me what the getch did and how the extras have fixed this, I'm studying so would like to make sure I know how things work!

Hi Frank,

There is no link with getch();
getch() - This is used to get a character from stdin.This is a nonstandard function that gets a character from keyboard, does not echo to screen.

Since the file is not found still and the file pointer returns NULL the line

fgets (message , 400 , fp);

is trying to read data from the file. That causes an assertion error.

In my modification, when the file not found i just exit from the main funtion using return statement. so the line

fgets (message , 400 , fp);

will not execute.

The below will make u understand clear,

#include<stdio.h>
#include<conio.h>
 
void main()
{
	
	char message[400];
	int i=0;

 	printf("This program will read in a file of your choice and print it out to the screen.\n\n");
 
	FILE *fp;
	fp=fopen("texuuts.txt","r");
 
	if (fp == NULL) 
		printf("File not found");
	else
	{
		printf("File opened!\n");
		fgets (message , 400 , fp); //This will cause assertion error when i used outside else part since its reading data from file poiner fp
		fclose(fp);
	}

	printf("\nYour text for encryption:\n");
        puts (message); // If the file not found this will prints garbage values.Try this
	getch(); //This is to wait for pressing any key to read the character but it wont display(echo) it.
}
This article has been dead for over six months. Start a new discussion instead.