I am working on an exercise that has me ask the user for the name of a text file and then display the just the first 10 lines of it. If there is 10 lines or less, I have to tell the user the entire file has been displayed. This is what I have written.
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
fstream dataFile;
string buffer;
string filename;
int count;
cout << "Please enter the name of the file: ";
getline(cin,filename);
dataFile.open(filename.data(), ios::in);
if (!dataFile)
{
cout << "The file " << filename << " could not be opened" << endl;
exit(1);
}
for (count = 1; count < 9; count++);
getline(dataFile,buffer);
cout << buffer[count] << endl;
dataFile.close();
}
I know there is a way to do it by just typing
Code:
getline(dataFile,buffer);
cout << buffer[count] << endl;
10 times but I want to do it this way. If I take out the loop the program works so I know that is the only problem.