When file is checked if its open, it will not open.
The file doesn't open when ran, as it says "Unable to open file" based on my test cases. Is there anyway to fix this?
Is there something I need to fix with the file io function (which I created at the bottom) or something to do inside of the if statement (checking if its open)
#include "header.hpp"
int main() {
string infile, outfile;
print();
readFilenames(infile, outfile);
ifstream fin(infile.c_str());
// check if file can be opened
fin.open(infile);
if (fin.is_open())
{
string name, smallName = "", largeName = "";
int id, numPeople = 0, smallId, largeId;
double balance, totalBalance = 0, largeBalance, smallBalance;
cout << "\n\nReading records from input file " << infile << endl;
cout << "Output records to output file " << outfile << endl;
ofstream fout(outfile.c_str());
print(fout);
fout << "List of Entries : " << endl;
printHeading(fout);
// read till the end of file
while (!fin.eof())
{
numPeople++;
getline(fin, name); // read name
name = name.substr(0, name.length() - 1); // remove '\n' from the end of name
// read id and balance
fin >> id >> balance;
}
} else
cout << "Unable to open file : " << infile << endl;
cout << "\nThank you for using my program";
return 0;
}
void readFilenames(string &infile, string &outfile)
{
cout << "What input file would you like to use? ";
getline(cin, infile);
infile = infile.substr(0, infile.length() - 1);
cout << "What output file would you like to use? ";
getline(cin, outfile);
outfile = outfile.substr(0, outfile.length() - 1);
}