how to read the text file exactly line by line?
i've been searching online for a long time and can not find the answer i am looking for. Here is the code I have currently:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
cout << "Which profile will you view\nView Profile: ";
string filename;
getline(cin, filename);
ClearScreen();
string _c_dir = "c:\\profiledata\\";
ifstream readfile;
readfile.open(_c_dir + filename);
string output;
if (readfile.is_open()) {
while (!readfile.eof()) {
readfile >> output;
cout << output << " "
}
}
readfile.close();
cout<<"Press Enter To Continue\n";
getchar();
|
I'm trying to get it to read the text file line by line! Instead it looks something like this:
First Name: Bob Last Name: Builder Age: # Information:infohere |
I want it to look like this:
First Name: Bob
Last Name: Builder
Age: #
Information: Info Here
|
Is there anyway to make it look like this at all? I'd really appreciate it if this problem I have can be solved.
Replace this:
1 2 3 4
|
while (!readfile.eof()) {
readfile >> output;
cout << output << " "
}
|
with this:
1 2 3
|
while (getline(readfile, output)) {
cout << output << endl;
}
|
Thanks! It worked.
Topic archived. No new replies allowed.