readFile function not working on second iteration with different argument – c++

  c++, function, ifstream

I’ve written a readFile function for a project I’m working on. I call it once, load in a file and read in it’s contents – works fine

However, when I try to load it a second time, attempting to change the file name – it loads it in, saves it to a static string ‘path’ that I access in a different function – but then the function is not printing the data

The question is, how do I change the file name, and read it in successfully on the second iteration? The part that has me stumped is that it works once, but not twice

Ive attempted to use cin.ignore(); cin.clear(); cin.sync() on the second iteration of fileName function – but none of them allow a separate file to be read successfully.

Minimum Reproducible Example:

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <vector>
#include <sstream>
#include <iomanip>
#include <iostream>

using namespace std;

static string path;
string opt;
void readFile();
int fileName();

void menu() { // put in while loop - while True
    cout << "----------------------" << endl;
    cout << "R(ead) -" << "Read File" << endl;
    cout << "F(ile) -" << "Set Filename" << endl;
    cout << "nPlease select from the above options" << endl;
    cin >> opt;
    cout << "nInput entered: " << opt << endl;
    if (opt == "R") {
        readFile();
    }
    if (opt == "F") {
        fileName();
    }
}

void readFile() { // doing this twice
    ifstream readFile;
    readFile.open(path);
    if (!readFile.is_open()) {
        cout << "Could not read file" << endl;
    }
    string str;

    int i = 0;
    while (getline(readFile, str))
    {
        if (str[0] != '/')
        {
            cout << "DEBUG: Line is - " << str << endl;
        }
    }
    readFile.clear();
    readFile.close();
    menu();
}

int fileName() {
    cout << "File path: ";
    if (path != "") {
        cin.ignore();
        cin.clear();
        cin.sync();
    }
    getline(cin, path);
    ifstream file(path.c_str());
    if (!file) {
        cout << "Error while opening the file" << endl;
        return 1;
    }
    cout << "(File loaded)" << endl;
    cout << "Path contains: " << path << endl;
    file.clear();
    file.close();
    menu();
}

int main()
{
    fileName();
}

Sample text, saved as txt file and read in using path:
Data1.txt

// standard test file
123,Frodo inc,2006, lyons,"1,021,000.16",0.0,
U2123,Sam Inc,2006, lyons,"21,600.00",13.10,123
A721,Merry Inc,2604, Kingston,"21,600.10",103.00,
U2122,Pippin Inc,2612, reid,"21,600.00",0
U1123,Huckelberry corp,2612, Turner,"21,600.00",13.10,

Data2.txt

7101003,Mike,23 boinig road,2615,48000,12000,0
7201003,Jane Philips,29 boinig cresent,2616,47000,12000,0
7301003,Philip Jane,23 bong road,2615,49000,000,0
7401004,Peta,23 bong bong road,2615,148000,19000,0
7101205,Abdulla,23 Station st,2615,80000,21000,0

The problem comes from reading in one, and trying to read in the other after the first has been executed.

  1. Enter Filename
  2. Hit Readfile
  3. Return to menu, hit Set Filename
  4. Change to Data2.txt
  5. Readfile again. Not working

My tutor told me "That’s not how functions work in c++" but didn’t elaborate further, and is unavailable for contact.

Source: Windows Questions C++

LEAVE A COMMENT