CppBuzz.com
  
Home C C++ Java Python Perl PHP Spring SQL Javascript Linux Forum

C++ program that reads in the lines from a text file and displays them with line numbers and a colon

You are here : Home » Solved Programs on C++ » C++ program that reads in the lines from a text file and displays them

Write a program that reads in the lines from a text file and displays them with line numbers and a colon. Limit the display to 24 lines at a time per the instructions. The output of the line numbers will be right aligned as shown below. The program may be handled in main.

Solution:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main(int argc, char** argv) {
	
	string filename;
	cout<<"\n Enter the file name : ";
	cin>>filename;
    
    ifstream readFile(filename.c_str());
    string line;
    unsigned long int lineNo = 1;
	while(getline(readFile, line))	
	{
		cout<<"\n"<<setw(3)<<lineNo<<":" << line;
		
		
		if(lineNo%24 ==0)
		{
			cout<<"\n\t Press ENTER to continue...";
			getchar();getchar();
		}
		lineNo++;
		
	}	
	
	
	return 0;
}