-
Reading text file
I need to display 24 lines of text from a file, then ask user to press enter and display 24 for lines, etc, etc, til end of file, but have no clue. My book doesn't give any advice on how to perform the operation.....Here's my code. Thanks for any clues.
Code:
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 5000;
char fileName[SIZE];
fstream file;
int count = 0;
cout << "Enter the file name:";
cin >> fileName;
file.open(fileName, ios::in);
file.getline(fileName,SIZE);
while (!file.eof())
{
count++;
cout<<count<<": ";
cout << fileName << endl;
file.getline(fileName, SIZE);
}
file.close();
system("PAUSE");
return 0;
}
-
1. getline terminates at newline, unless otherwise specified.
2. Don't check for eof().... instead check for the valdity of the read function.
3. Why were you counting? Look at your code.... what would a computer do?
Code:
int main()
{
//Please enter file name
//ifstream object
for(24 times)
{
getline(ifstream_object, stringbuf);
cout << stringbuf << endl;
}
}