user3807849
user3807849

Reputation: 1

using multiple functions in a fstream/ifstream file?

I've been browsing the web for hours hoping I can answer this question. I'm a C++ beginner and working on a program that reads a txt file and performs some functions to modify it. So far it can read and run the first function but functions after that cannot read the file/get executed. I've tried passing it as a reference and checking the functions for errors but so far only one function is executed regardless of the order.

Here is my code. Any help will be greatly appreciated.

//Reading and processing the text file
fstream text_file;
text_file.open (argv [2], ios::in | ios::out | ios::app);

if (!text_file.is_open())
{
    cout << "File is not open! " << endl;   //Checks whether the file is open
}
else 
{
toAlpha (text_file);        //This function executes
printFile (text_file);      //This doesn't

checkDictionary (text_file, Dictionary, Spelling);  //same for this one 
text_file.close ();
}

here is one of the functions I'm using that don't get executed

//Prints off the lines read in the txt file 
    void printFile (fstream& text_file)
    {
        string word;
        while (getline (text_file, word))
        {
            cout << word << endl;
        }
    } 

Upvotes: 0

Views: 645

Answers (1)

P0W
P0W

Reputation: 47804

Reset the file pointers and clear flags

toAlpha (text_file); 
text_file.seekg(0) ; 
text_file.clear();
printFile (text_file);   
text_file.seekg(0) ;
text_file.clear();
checkDictionary (text_file, Dictionary, Spelling);

Upvotes: 2

Related Questions