Reading from file and crashing

This is a discussion on Reading from file and crashing within the C++ Programming forums, part of the General Programming Boards category; Hello, I am currently working on a parser for my program but I am having problems with reading from file. ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    13

    Reading from file and crashing

    Hello,

    I am currently working on a parser for my program but I am having problems with reading from file. Please keep in mind that I am new to C++ and especially streams.

    This is how it goes:
    1. I open the file, and do a quick parse to count the number of objects defined in it.
    2. I create an array to hold the expected number of objects based on the count.
    3. I go through the file again to parse it in detail while creating the objects and storing them.

    Problem is that this does not work. I used to close the file after the count, reopen it for the reading. But the problem is that it never goes through the read loop. It's as if the pointer is still at the end of the file. To verify this, I switched them around and the same situation happened. Whichever loop is first is fine but the other one does not happen.

    So then I resorted to using clear() and seekg() but then my program goes through a major crash (something about Damage after CRT block). I'll be posting here the code in hopes that someone can enlighten me as to what causes the problem.

    Code:
    Parse(char *Filename)
    {
            ASSERT(Filename != NULL);
    	if(Filename == NULL)
    	{
    		//handle it later
    		return;
    	}
    
    	ifstream fin(Filename);
    	char charline[80];
    	int nb_figures = 0;
    	int nCurrentFigure = 0;
    
    	//Count the number of figures
    	if(fin)
    	{
    		while(fin.getline(charline, 80, ' '))
    		{
    
    			if(charline[0] == '/' && charline[1] == '/')
    				fin.ignore(80, '\n');			//Skip the entire line as it is a comment
    			else
    			{
    				if(charline[0] == 'L')
    					nb_figures++;
    				else if(charline[0] == 'T')
    					nb_figures += 3;
    				else
    					fin.ignore(80, '\n');
    			}
    		}
    	}
    	//fin.close();
    
    	m_lines = new CLine[nb_figures];
    
    	fin.clear();              // forget we hit the end of file
    	fin.seekg(ios::beg);   // move to the start of the file
    
    	//fin.open(Filename);
    
    	while(fin.getline(charline, 80, ' '))
    	{
    		if(charline[0] == '/' && charline[1] == '/')
    			fin.ignore(80, '\n');			//Skip the entire line as it is a comment
    		else
    		{
    			if(charline[0] == 'L')
    			{
    				ParseLine(&fin, nCurrentFigure);
    				nCurrentFigure++;
    			}
    			else if(charline[0] == 'T')
    			{
    				ParseTriangle(&fin, nCurrentFigure);
    				nCurrentFigure += 3;
    			}
    			else
    				fin.ignore(80, '\n');
    		}
    	}
    	fin.close();
    }
    I did not provide all the info as the code is very long. I include <fstream> and I have been using the std namespace.

    Thanks in advance

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > if(charline[0] == '/' && charline[1] == '/')
    > fin.ignore(80, '\n'); //Skip the entire line as it is a comment
    Does this mean you comment spans two lines, like this?

    //
    this is a comment

    This seems equivalent to me, if you remove all the extra ignores which seem to be there
    Code:
    while(fin.getline(charline, 80, ' '))
    {
        if(charline[0] == 'L')
          nb_figures++;
        else if(charline[0] == 'T')
          nb_figures += 3;
      }
    }
    > ParseLine(&fin, nCurrentFigure);
    Shouldn't you be parsing the line you just read, not the file?

    Also, your 2nd while loop is OUTSIDE your if ( fin ) test

    Which compiler are you using?
    I seem to remember one particular compiler having problems rewinding the stream for another pass.

    > I did not provide all the info as the code is very long
    Creating a new file with a simple main() and the function in question often tells you what is wrong, and is handy to paste to the board if it doesn't.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-03-2005, 10:41 PM
  2. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  3. read from .txt file & put into array?
    By slow brain in forum C Programming
    Replies: 6
    Last Post: 02-25-2003, 04:16 AM
  4. Reading a PCX file into memory
    By SMurf in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 11:54 AM

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