Thread: Reset to Beginning of file

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    20

    Reset to Beginning of file

    Im doing some simple fileinput.. I am having issues resetting the file read to the start of the file. Basically If I run the function twice it won't work, once is fine though! :/ . I tried .seekg() but that didn't seem to work either. I am probably doing something arseways.. Anybody got any clues.

    Code:
    #include <iostream>
    #include <string>
    
    // The next line is required for input and output file IO
    #include <fstream>
    
    using namespace std;
    
    void main()
    {
    	
    
    	string line,songCount,songChoice,title;
    	bool printOut = false;
    	int exitLoop = 0;
    	int menuChoice = 0;
    	
    	ifstream fileInput;
    	fileInput.open("hnr0.abc", ios::in);
    
    	cout << "Trad Music Selector:" << endl << endl;
    
    	do
    	{
    		cout << "Menu Options: \n 1. Print List of songs \n \n enter choice: " ;
    		cin >> menuChoice ;
    		switch(menuChoice){
    
    			fileInput.close();
    			fileInput.open("hnr0.abc", ios::in);
    
    			case 1:
    				
    
    
    					while(!fileInput.eof())
    					{
    						
    						getline(fileInput,line);//read a line from data.txt and put it in a string
    
    						
    						/*if the first two characters X: print a blankline to seperate titles.
    						After X: is found print all titles on the same line seperated by :*/
    						
    						if(line.find("X:")==0)
    						{
    							cout << endl << endl;
    						}
    						else if(line.find("T:")==0)
    						{
    							title = line.substr(2,string::npos);
    							cout << title <<":" ;
    						}
    
    					}
    				
    				cout << endl << endl;
    
    
    				break;
    
    			
    		}
    
    
    
    		}while(exitLoop!=1);
    
    
    	
    }

  2. #2
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Seekg should work:

    Code:
    fileInput.seekg(0, ios_base::beg);

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    void main() is incorrect in C++. It's always int main().

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    switch(menuChoice){
    
    			fileInput.close();
    			fileInput.open("hnr0.abc", ios::in);
    
    			case 1:
    The switch will just jump straight to the case label.

    Instead you might just declare and open the file under the first case. (You'll need braces around the code, since you will be introducing new variables.)

    Code:
    switch(menuChoice){
        case 1:
        {
             std::ifstream fileIn("hnr0.abc");
             std::string line;
             while (getline(fileIn, line)) {
                    //...
             }
        }
         break;
        //other cases
    }
    Also note that you shouldn't use eof() to control the input loop (may end up "reading" the last line twice).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    I may be mistaken, but using seekg to reset the read pointer to the start of file won't help you start from the beginning of the file because the end of file error bit is automatically set when you reach the end of the file. If you want to read the file again, you will need to use seekg to change the position of the pointer and then use the clear() method to reset the eof bit. You have to do it in that order because if you used the clear method first then the eof bit is automatically changed and so you don't get anywhere.

    Here is some info about the clear() method. If you have any more problems just post again.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    20
    closing and reopening the file worked

    using
    clear()
    before, not after
    seekg(ios::beg) also worked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM