Thread: Debug Assertion Failed error

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    13

    Debug Assertion Failed error

    Hi, I've been having problems with this code, a few people have looked at it and say it should work but I get a 'Debug Assertion Failed' error. It says 'expression str!=NULL'.

    Not sure what the problem is, here is the segment that seems to contain the error.

    Code:
    	FILE *fp;	
    	fp=fopen("texts.txt","r");
    
    	if (fp == NULL) 		
    		printf("Can't open file!\n");	
    	else		
    		printf("File opened!\n");          
    	
    
    	fgets (message , 400 , fp);	
    	
    	fclose(fp); //all file use is complete

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    What is message variable? Where is it declared and how much memory does it hold? Have you included the header file properly?

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    13
    Here is the complete code, the message variable has been declared and holds 400 chars so it should be fine.

    Code:
    #include<stdio.h>
    #include<string.h> 
    #include<ctype.h>
    
    void main()
    {	
    	
    	/*declare and initialise variable*/	
    	char message[400];	
    	int i=0;	
    	char filename[30]; 
    	char *lower;
    	
    	/*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("upper.txt","r");
    
    	if (fp == NULL) 		
    		printf("Can't open file!\n");	
    	else		
    		printf("File opened!\n");          
    	
    
    	fgets (message , 400 , fp);	
    	
    	fclose(fp); //all file use is complete 
    
    	/*Converts each char in the array to lower case*/
    	for (lower=message;*lower!='\0';++lower)
       {
           *lower=tolower(*lower);
       }
    
    	/*Prints text*/
    	printf("\nYour text for encryption:\n");    
    	puts (message);
    
    }

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    The code seems to working fine for me. Where excatly does your code crashed, are you able to read the file name. Though your not using filename user entered.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    13
    Quote Originally Posted by ssharish2005 View Post
    The code seems to working fine for me. Where excatly does your code crashed, are you able to read the file name. Though your not using filename user entered.

    ssharish
    Hi,

    I'm able to read in fine, it crashes after the user input the filename (I know I'm not using it yet, I have no idea how to do it! Still trying to figure it out).

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    If you can't open the file you should not continue on in your program as if it were opened successfully, which you are currently doing.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    ok this is interesting. Could you copy is the excat error message and post it here.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 'Debug Assertion Failed' error. It says 'expression str!=NULL'.
    And it should also show a filename, which would give some indication of the function which failed.

    It looks like you failed to open the file.

    Whilst you do have an fp == NULL check, that does NOT stop you from trying to read the file anyway.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM