Home > Software engineering >  Having trouble figuring out this issue I'm having with getline() for file IO
Having trouble figuring out this issue I'm having with getline() for file IO

Time:11-19

I'm trying to open a file, display 20 lines at a time until the file ends, and then close the program. Real easy.

Here is the code:

int main()
{
    using namespace std;

    //try catch block sets up exception handling for files that do not exist. I added a few different
    //text files to search for
    try {
        //intro message and prompt for file name
        cout << "Welcome to the Advanced File I/O program." << endl;
        cout << "Please enter the name of the text file you would like to search for: ";
        string input;
        cin >> input;
        //concatonate input with .txt file designation
        string fileName = input   ".txt";
        fstream myFile;
        //open the file and read it in
        myFile.open(fileName, ios::in);
        //if the file exists
        if (myFile.is_open()) {
            string line;
            //while there are lines to be read in
            while (getline(myFile, line)) {
                
                //display 20 at a time
                for (int i = 0; i < 20 && getline(myFile, line); i  ) {
                    cout << line << endl;
                }
                //app console controle
                system("pause");
                system("cls");
            }
            //app console controle
            system("pause");
            system("cls");
            //close the file once it's all read in and display end message
            myFile.close();
            system("cls");
            cout << "You have reached the end of the text file. " << endl;
            cout << "Thank you for visiting! Goodbye!" << endl;
            system("pause");
            exit(0);


        }
        //if the file does not open (does not exist) throw the error exception and close the program
        else if (myFile.fail()) {
            throw exception("File does not exist. Closing Program.");
            cout << endl;
            system("cls");
            exit(0);
        }
    }
    //catch the exception and display the message
    catch (exception& e) {
        cout << "\nError caught!" << endl;
        cout << e.what();
    }
}

The problem is that it is skipping the first line on the output every time in the for loop. I'm pretty sure that is happening because I'm calling getline() twice. However, I have no idea how to fix this issue.

For those who are going to tell me to not use using namespace std;, I am required to for the course this assignment is in.

CodePudding user response:

I just had to print the line after the initial getline() call in the while loop.

CodePudding user response:

Your outer while loop is reading a line from the file and discarding it without displaying it. Then, your inner for loop reads the next 20 lines and displays them. Then the next iteration of your outer while loop reads and discards the next line. And so on.

Try something more like this:

int main()
{
    using namespace std;

    try {
        cout << "Welcome to the Advanced File I/O program." << endl;
        cout << "Please enter the name of the text file you would like to search for: ";
        string fileName;
        getline(cin, fileName);
        fileName  = ".txt";

        ifstream myFile(fileName);
        if (!myFile.is_open())
            throw exception("File not opened. Closing Program.");

        string line;
        while (getline(myFile, line)) {
            system("cls");
            cout << line << endl;
            for (int i = 1; i < 20 && getline(myFile, line);   i) {
                cout << line << endl;
            }
            system("pause");
        }

        myFile.close();

        system("cls");
        cout << "You have reached the end of the text file. " << endl;
        cout << "Thank you for visiting! Goodbye!" << endl;
    }
    catch (const exception& e) {
        system("cls");
        cout << "\nError caught!" << endl;
        cout << e.what() << endl;
    }

    system("pause");
    return 0;
}
  • Related