Assertion Error

This is a discussion on Assertion Error within the Windows Programming forums, part of the Platform Specific Boards category; I'm back again, with another fgets issue (sort of). I've used this code numerous times, as it's quite standard, but ...

  1. #1
    tao
    tao is offline
    Registered User
    Join Date
    Jun 2006
    Posts
    26

    Assertion Error

    I'm back again, with another fgets issue (sort of).

    I've used this code numerous times, as it's quite standard, but for some reason I'm getting an error now.

    Code:
    Debug Assertion Failed
    File: fgets.c
    Line: 60
    
    Expression: str != NULL
    And the parcel of code giving me the error (actually, I isolated it to just the while(fgets()!=NULL) loop - the code inside the loop has no influence).

    Code:
    FILE *cFile;
    char Line[100];
    
    // Check for match before opening file
    if(strcmp(STRTOCOMP1, STRTOCOMP2) == 0)
    {
    	cFile = fopen(FILENAME, "r");
    
    	// If the file cannot be read, warn the user
    	if(!inp)
    	{
    		MessageBox(Msg, "Error", MB_OK | MB_ICONEXCLAMATION);
    		return;
    
    	// If the file can be read, proceed to read it in
    	} else
    	{
    		while(fgets(Line, sizeof Line, cFile) != NULL)
    		{
    			// sscanf() stuff to a struct vector
    		}
    
    		fclose(cFile);
    	}
    
    	// If there was no match, warn the user
    } else
    {
    	MessageBox(Msg, "Error", MB_OK);
    }

    The code seems flawless -- why would it be throwing an assertion error? Unless I'm missing something completely obvious, which is kind of why I'm asking for help.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Can you post the actual code with str and inp and stuff?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    Code:
     cFile = fopen(FILENAME, "r");
    
    	// If the file cannot be read, warn the user
    	if(!inp)
    Helps if you check the actual variable, not something else.

    > Expression: str != NULL
    It's basically telling you that a parameter which should be != NULL is in fact failing that test (ie, it is NULL).
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    tao
    tao is offline
    Registered User
    Join Date
    Jun 2006
    Posts
    26
    I found that this morning when I took a fresh look at the code. Thanks for the let-know, though. Funny how something so menial can slip me by so easily.

  5. #5
    tao
    tao is offline
    Registered User
    Join Date
    Jun 2006
    Posts
    26
    I have another question regarding sscanf as part of the above code; thought I'd throw it in here as opposed to cluttering up the place with a new thread.

    I'm trying to read in a file and parse the lines with sscanf, but for some reason when I do the assignment the variables are all assigned the same parse snippet.

    Below is the code involved:

    Code:
    struct ConfigFile
    {
    	int Label;
    	CString STR1;
    	CString STR2;
    	CString STR3;
    } Temp;
    
    std::vector <ConfigFile> vConfig;
    Code:
    while(fgets(Line, sizeof Line, cFile) != NULL)
    {
    	if ( sscanf(Line, "%d %s %s %s", &Temp.Label, Temp.STR1, Temp.STR2, Temp.STR3) == 4)
    		vConfig.push_back(Temp);
    }

    The file contains a three-digit int, followed by three strings (simple strings with no spaces), all divided by whitespace (tabs or spaces only).

    ex.
    123 STRING1 STRING2 STRING3
    256 STRING4 STRING5 STRING6

    etc.

    My output is giving me:
    123
    STRING3
    STRING3
    STRING3


    CStrings STR1, STR2, STR3, they are all being assigned the same value for some reason. I have absolutely no idea why; but I'm rather new at much of this, so that isn't surprising.

    Anybody have any suggestions / solutions? Thanks.

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Strange feeling that scanf can not work with CString's.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    > CStrings STR1, STR2, STR3, they are all being assigned the same value for some reason
    Because %s only knows about char arrays, not CStrings.
    I've no idea what they are, probably some locally defined C++ class perhaps?
    So why aren't you using getline() and string streams to extract information?

    Use local arrays, like so, if you insist on sscanf
    Code:
    	char t1[100], t2[100], t3[100];
    	if ( sscanf(Line, "%d %s %s %s", &Temp.Label, t1, t2, t3) == 4) {
    		Temp.STR1 = t1;
    		Temp.STR2 = t2;
    		Temp.STR3 = t3;
    		vConfig.push_back(Temp);
    	}
    That is, assuming that your Cstring class has an overloaded assignment operator which does the right thing...
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    tao
    tao is offline
    Registered User
    Join Date
    Jun 2006
    Posts
    26
    I was wondering if it was perhaps the CString type that was causing conflict. I'll try your suggestion, Salem.

    Edit: Yes, that worked like a charm. Thanks for the input. =]

    As to why I chose sscanf as opposed to getline, I felt it was a faster way to parse the data instead of manually skipping whitespace (since the data file has a predefined structure, I don't really require a lot of error checking).
    Last edited by tao; 06-19-2006 at 07:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  3. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  4. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  5. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21