checking if a file is empty

This is a discussion on checking if a file is empty within the C++ Programming forums, part of the General Programming Boards category; ok, i have a program that checks to see if a .txt file exists, and, if not, creates the file. ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    17

    checking if a file is empty

    ok, i have a program that checks to see if a .txt file exists, and, if not, creates the file. this file needs to be used by multiple instances of the program running simultaneously. each instance of the program uses this file to read in data to a linked list. however, if the file exists but has no text (this happens when one instance just created the file but hasn't added any text yet and a second instance starts up), the second instance tries to read from the empty file and crashes. i have a while loop - while ( !reader.eof() ) - that i didnt think it would enter: i thought that an empty file would immediately trigger the eof() and the program would just skip the loop..... apparently not. can anyone help?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You generally shouldn't use eof() to control the while loop. Instead put the read operation into the loop control (e.g. while (reader >> var)).

    That may not be the problem, though. An empty file should set eof() to true. Maybe you can show more code.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    17

    more code

    alright, heres the relevant code:

    Code:
    CUserList :: CUserList ()
    {
    	m_Reader.open ( USERLIST );
    	
    	cout << "try open" << endl;
    	
    	if ( !m_Reader )
    	{
    		cout << "open failed" << endl;
    		m_Writer.open ( USERLIST );
    		m_Writer.close();
    		m_Reader.open ( USERLIST );
    		m_EmptyFile = true;
    	}
    	else
    		m_EmptyFile = false;
    	
    	readFile();
    }
    
    
    void CUserList :: readFile ()
    {
    	char temp [ MAXLINE ];
    	string tempStr;
    	while ( !m_Reader.eof() && !m_EmptyFile )
    	{
    		m_Reader.getline ( temp, MAXLINE );
    		tempStr = temp;
    		m_Connections.AddNode ( tempStr );
    	}
    }
    the m_EmptyFile i threw in there to take care of the first time the file is created.... if this instance of the program created the file, then it's going to know it's empty, obviously.... but this obviously didn't solve my problem either, i realize, so that's expendable at this point.

    ... there could be plenty more problems.... i'm not the most experienced programmer, so any help is appreciated

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    When an instance opens the file for writing it should probably open it in exclusive mode so that other instances or processes cannot access it. It would be a terrible misstake for the 1st instance to start writing to the file and the 2nd instance read from it at the same time because the 2nd instance may get only partial data.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    thanks for the suggestion ancientdragon, i'll do that..... but i also know that isnt where my current problem lies, as this was happening even w/ only a single instance of the program....

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I don't see why that specifically would cause a crash, but the ins and outs of eof() are a bit confusing. Regardless, this is how I would do that same loop:
    Code:
    void CUserList :: readFile ()
    {
    	if ( m_EmptyFile )
    		return;
    
    	string tempStr;
    	while ( getline ( m_Reader, tempStr ) )
    	{
    		m_Connections.AddNode ( tempStr );
    	}
    }
    You don't have to move the m_EmptyFile check if you don't want. However, notice how I put the call to getline inside the while control. If and only if it succeeds in reading a line, it will add that line to m_Connections. Also notice the different use of getline. There is a getline that works for strings, so there is no need for the temp character array. It is safer, easier, and cleaner to get rid of it.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    awesome.... works perfectly. thanks a lot daved, much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 10:05 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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