for example, there are 100 lines from the file, and I only want to show 20 lines and ask the user to press enter and then another 20 line display and so on to the end of the file.
here is my code
int main()
{
ifstream infile;
string filename;
string line;
int lineCount = 0;
cout << "Enter file name: \n";
cin >> filename;
system("cls");
infile.open(filename);
if (!infile)
{
cout << "\nErrors\nNo file exist.\n";
exit(1);
}
else
{
while (infile.good())
{
lineCount++;
getline(infile, line);
cout << lineCount << ":\t" << line << endl;
}
}
return 0;
}
You just have to count the lines. Example:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream infile;
string filename;
string line;
int lineCount = 0;
cout << "Enter file name: \n";
cin >> filename;
system("cls");
infile.open(filename);
if (!infile)
{
cout << "\nErrors\nNo file exist.\n";
exit(1);
}
else
{
while (getline(infile, line))
{
cout << lineCount << ":\t" << line << endl;
if (lineCount % 20 == 0) {
cout << "Press Enter To Read 20 More Lines, Or Type Quit to Exit";
string response;
getline(cin, response);
if (response == "Quit") return 0;
}
lineCount++;
}
}
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.