I am trying to read an input file onto my screen and am having issues doing it. I am just trying to ask the user to tell me the file they want to open. Then open and see whats inside. I got a pro tip saying I am reading from the keyboard not the file but I am not sure how to change that.
//Reading a file 24 lines at a time
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
usingnamespace std;
int main ()
{
//filename = what the file is actually called on the computer
string filename;
string word;
//PFname = what the program is calling the file
fstream PFname;
cout << "What file would you like to open? \n(Dont forget to add the extenstion e.g. ReadingAFile.cpp)";
getline(cin,filename);
PFname.open(filename.c_str() );
//if must be name of what the program calls the file
if (!PFname)
{
cout << "\nNo file of that name exists ";
}
else
{
cout << "In the else";
getline(cin,word);
while (!PFname.eof())
{
cout << word;
getline(cin,word);
}
}
PFname.close();
return 0;
}
Can somone help me out and explain what this while loop is doing. It is supposed to read in a file and it does that but it doesn't do the entire while loop seems like it only does line 6. In other words it doesn't use the variable count.