Trouble with first part of my program
Oct 31, 2018 at 11:59pm UTC
Need help with this issue. I am having in the start of my program. I ask the user to enter a file they would like to select to open, then after they select it my file just closes instead of reading off the lines in my txt file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
string fileName=" " ;
string line;
do {
cout << "Please enter a file name." << endl;
getline(cin, fileName);
} while (fileName != "file1" && fileName != "file2" && fileName != "file3" );
cout << "Reading File... Done" << endl;
if (fileName == "file1" ) {
ifstream file1("input1.txt" );
if (file1.is_open()) {
while (file1.good()) {
getline(file1, line);
cout << line << endl;
}
file1.close();
}
}
Nov 1, 2018 at 1:27am UTC
Let's start small.
What does this program print for you?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <fstream>
#include <iostream>
int main()
{
using namespace std;
ifstream file("input1.txt" );
if (file)
{
cout << "File opened!" << endl;
}
else
{
cout << "Failed to open file" << endl;
}
}
Topic archived. No new replies allowed.