Thread: error "debug assertion failed"

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    error "debug assertion failed"

    I was trying out this program but it terminates by saying that debug assertion has failed.

    [insert]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void){
    
    	FILE *fp;
    	char ch;
    	int nol= 0, not = 0, nob = 0, noc =0;
    
    	fp = fopen("C:\Documents and Settings\ROHAN\Desktop\DESKTOP\filejava.txt", "r");
    
    	while(1){
    	
    		ch = fgetc(fp);
    
    		if(ch == EOF)
    			break;
    
    		noc++;
    
    		if(ch = " ")
    			nob++;
    
    		if(ch = '\n')
    			nol++;
    
    		if (ch = '\t')
    			not++;
    	}
    
    	fclose(fp);
    	printf("\n No of char = %d", noc);
    	printf("\n No of line = %d", nol);
    	printf("\n No of tab = %d", not);
    	printf("\n No of space = %d", nob);
    
    	return 0;
    }
    What is the error and what does this imply "debug assertion failed"? I am using Visual Studio c++.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Ha!

    Inside your string literal, you need to double your backslashes. (Backslashes are escape char's in C).

    Depending on your compiler variables, you may want to specify text file mode. I'm always a bit amazed that people so frequently open a file without specifying the file mode they want.
    Last edited by Adak; 08-06-2009 at 11:19 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You didn't check the fopen() call succeeded.

    Also, watch the use of \\ vs \ in your pathnames.

    Better still, use a single / instead.
    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.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    I made the modifications but instead of printing out the exact characters, tabs, new lines everything seems to come out equal to 140. But in the actual file this is not the case.( i havent counted it , but it has only around 6 lines and each lines is having around 15-20 characters.

  5. #5
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    ...and just a quick suggestion, you could use

    Code:
    while((ch = fgetc(fp)) != EOF)
    {
    ...
    instead.

    edit:
    i havent counted it , but it has only around 6 lines and each lines is having around 15-20 characters.
    if(ch = " ")
    You are using single '=' istead of '=='!!!
    Last edited by GL.Sam; 08-06-2009 at 11:29 PM.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  6. #6
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Also what does the error "debug assertion failed" had to do with me having a single \ instead of \\ in the file path that i gave initially.

  7. #7
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    And last, but not least: declare ch as int when you have to deal with EOF. Believe me.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  8. #8
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Yes it works. Also to check blank spaces i was using

    if (ch = " "); // i am using the double quotes instead of single quotes.

    But how come converting ch to int does the work and not keeping it as a char?

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by roaan View Post
    Also what does the error "debug assertion failed" had to do with me having a single \ instead of \\ in the file path that i gave initially.
    That was the first error in your program. Clearly, it wasn't the last. For more info on your "debug assertion failed" error, just paste that phrase into your VS search text box, and click on the search through your help, icon.

    VS compiler will make certain assertions in an effort to compile and run your debug version, even when the release version (which would have no such assertions from the compiler), can't run or compile. The error message is not specifying which of those assertions has failed. It only makes sense to fix every error in your program, doesn't it?

    EOF is a number (usually -1), not a char.

  10. #10
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by roaan View Post
    But how come converting ch to int does the work and not keeping it as a char?
    char has a range of -128 to 127, so if for eg. any character whose ASCII value is 255 comes in the file, it'll be interpreted as -1, coz of its range and the condition ch==EOF will be true(which actually is not true). So to increase the range of ch, int ch is advisable, as all the chars will be accomodated in it and there will not be any misinterpretation.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Most likely, the file did not open because you didn't escape your backslashes. This would result in a NULL result from fopen(). Then, the call to fgetc() triggers this debug assertion because you passed it a NULL pointer. That would be my first guess, since you do not have any explicit calls to assert() yourself.

    The MSVC runtime, when compiled in debug mode, has lots of these sorts of checks. In a non-debug build, it would probably just crash instead.

    Also, this line of code:

    Code:
    if(ch = " ")
    Should be this instead:

    Code:
    if(ch == ' ')
    Notice the double '=='. The other two lines should also use '==' instead of '='
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by BEN10 View Post
    char has a range of -128 to 127, so if for eg. any character whose ASCII value is 255 comes in the file, it'll be interpreted as -1, coz of its range and the condition ch==EOF will be true(which actually is not true). So to increase the range of ch, int ch is advisable, as all the chars will be accomodated in it and there will not be any misinterpretation.
    I really did not get what you intended to say. Can you make it more clearer.
    signed char has a range of -128 to 127 but unsigned char has a range of 0 to 255. And if we get a char whose ASCII value is 255 it will get stored in
    char ch;
    as the actual character instead and not as -1. Hope i am right .......

  13. #13
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    255 will get stored as -1 in char ch and not as 255 if it's signed.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by BEN10
    char has a range of -128 to 127
    Actually, char has the same range as signed char or unsigned char, the choice depending on the implementation.

    Quote Originally Posted by roaan
    signed char has a range of -128 to 127 but unsigned char has a range of 0 to 255. And if we get a char whose ASCII value is 255 it will get stored in
    char ch;
    as the actual character instead and not as -1.
    Perhaps it would be easier to let the C standard speak for itself if you really don't want to trust GL.Sam. Concerning EOF:
    The header <stdio.h> declares three types, several macros, and many functions for performing input and output.

    (...)

    The macros are (...)

    EOF
    which expands to an integer constant expression, with type int and a negative value, that is returned by several functions to indicate end-of-file, that is, no more input from a stream;
    Concerning fgetc:
    #include <stdio.h>
    int fgetc(FILE *stream);

    Description
    If the end-of-file indicator for the input stream pointed to by stream is not set and a next character is present, the fgetc function obtains that character as an unsigned char converted to an int and advances the associated file position indicator for the stream (if defined).

    Returns
    If the end-of-file indicator for the stream is set, or if the stream is at end-of-file, the end-of-file indicator for the stream is set and the fgetc function returns EOF. Otherwise, the fgetc function returns the next character from the input stream pointed to by stream. If a read error occurs, the error indicator for the stream is set and the fgetc function returns EOF.
    Notice that "the fgetc function obtains that character as an unsigned char converted to an int", and that it can also return EOF, which is outside of the range of an unsigned char.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Yeah, let me correct myself that char ch will be either signed or unsigned entirely depending on the implementation.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  2. Assertion Error with DoModal(), activated from a thread
    By hanhao in forum Windows Programming
    Replies: 1
    Last Post: 07-08-2005, 12:39 AM
  3. Assertion error when looking for window handle
    By axr0284 in forum Windows Programming
    Replies: 3
    Last Post: 02-03-2005, 04:11 PM
  4. debug assertion failed !
    By blue_gene in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2004, 11:23 AM
  5. Assertion Failure
    By drdroid in forum Game Programming
    Replies: 9
    Last Post: 01-04-2003, 07:02 PM