Thread: fgets crashing my program

  1. #1
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790

    fgets crashing my program

    even though this code is from a game engine i am making, the problem itself is base C++ oriented so i though i would post it here.

    Code:
    void readstr(FILE *f,char *string)
    {
    	do								
    	{
    		fgets(string, 255, f);		
    	} while ((string[0] == '/') || (string[0] == '\n'));		
    	return;								
    }
    
    
    
    tMap LoadMap(char *szFileName)
    {
    tMap Map;
    FILE *file;
    char oneline[255]; // one line of text
    int nNumTiles;
    float x,y,u,v;
    int tID,objID, nSol,nRows, nColumns;
    int xSize,ySize;
    
    file = fopen(szFileName,"rt");
    
    readstr(file,oneline);
    
    sscanf(oneline, "NUMTILES %i %i %i %i %i\n", &nNumTiles, &nRows, &nColumns, xSize, ySize);
    
    Map.nNumRows    = nRows;
    Map.nNumColumns = nColumns;
    Map.txSize      = xSize;
    Map.tySize      = ySize;
    Map.tTiles      = new tTile[nRows * nColumns];
    
    for(int nTiles =0; nTiles < nNumTiles; nTiles++)
    {
    
    for(int nVertice = 0; nVertice < 6; nVertice++)
        {
              readstr(file,oneline);
              sscanf(oneline,"%f %f %f %f\n", &x,&y,&u,&v );
              
              Map.tTiles[nTiles].vVertex[nVertice].x = x;
              Map.tTiles[nTiles].vVertex[nVertice].y = y;
              Map.tTiles[nTiles].vVertex[nVertice].u = u;
              Map.tTiles[nTiles].vVertex[nVertice].v = v;
        }
        
    
    readstr(file,oneline);
    sscanf(oneline,"%i %i %i\n", &tID, &objID,&nSol);
    
    Map.tTiles[nTiles].tID = tID;
    Map.tTiles[nTiles].objID = objID;
    
    if(nSol == 1){
    Map.tTiles[nTiles].solid = true;
    }
    else if(nSol == 0)
    {
    Map.tTiles[nTiles].solid = false;
    }
    
    }
    
    fclose(file);
    return Map;
    };
    ok, so, I have isolated the problem to fgets itself, not the loop that it is in by just using fgets by itself in the function. Why would fgets crash the program, when the file loads, and the only thing i find is that just by running the function, it crashes. I know the fgets works because it does load up the number of tiles(i did have a few error checking functions in there), however, it is the only thing i think could be causing this problem, because i have limited the function to only opening the file and using fgets and it crashes.

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Change char oneline[255] to char oneline[256], just in case.

    Your fgets loop doesn't check for the end of the file.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You need to check the return codes from fopen and fgets before going any further.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "Change char oneline[255] to char oneline[256]"

    Incorrect.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Change char oneline[255] to char oneline[256], just in case.
    Eh? As displayed by the comment following that line of code:

    // one line of text

    How can you determine that a line is 256 instead of 255 characters in length. I would be more interested in the size of the stream buffer since that would be a more likely upper limit for the line. Of course, since this input is coming from a file there's no telling how long a line could be and indeed there's no real limit (barring actual physical memory of course). So without knowing the format of the file, the array's size is likely to be arbitrary.
    My best code is written with the delete key.

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Originally posted by Prelude
    >Change char oneline[255] to char oneline[256], just in case.
    Eh? As displayed by the comment following that line of code:

    // one line of text
    The parameter on his fgets statement is 255. I assumed it didn't have the room for the null terminator, so I said change his definition from oneline[255] to oneline[256] just in case.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    fgets() allows for 1 byte null terminator, so passing the actual array size is the correct thing to do (normally).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    well, i found i was doing wrong on my fopen error check, and in fact, fopen is the function not working at all, even trying varieties of the file's name, it still comes up with a null pointer, which is the reason fgets didnt work. what could be the possible reason for fopen not to work, when reading a file(it works when creating a new file in this situation).

    EDIT: I found out the problem, by entitling my text file "Terrain.txt" it was making the actual filename "Terrain.txt.txt " and therefore was not opening when i was passing "Terrain.txt" to my function.
    Last edited by EvBladeRunnervE; 08-11-2003 at 12:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program crashing when I use IO
    By legit in forum C++ Programming
    Replies: 9
    Last Post: 05-31-2009, 07:54 AM
  2. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM