Numbering lines inside a txt file

This is the code i have to display the files, but now i need help number each individual line inside the file.. . . can anyone help me?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  #include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    ifstream file;
    string input;

    cout << "Enter filename" << endl;
    cin >> input;

    file.open(input.c_str());

    if (!file)
    {
        cout << "File not found." << endl;
        return 0;
    }
    else if (file.is_open())
    {
        getline(file, input);
        cout << input << endl;
    }

    file.close();

    return 0;
}
1. Read a line
2. Print a number++ and the line
3. Repeat
clarify those steps in greater detail please.
Why?

You already show that you can do step 1.
Step 2 is merely a variation of what you already do too.
Step 3, seriously, do you claim that you have never encountered a control structure that can repeat statements?
yeah,i know how to do loops... i guess i have to think about it more, and not ask for help in the beginning. ty <3
1
2
3
4
5
    else if (file.is_open())
    {
        getline(file, input);
        cout << input << endl;
    }


You may wish to use a loop and increment the counter each time (for each line).
Thank you, this is helping a lot.
this is what i have now. . . all i need is to number each line from here,


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
ifstream file;
string input, String;
int a = 0;
string prev_line = "";
int counter = 0;

cout << "Enter filename" << endl;
cin >> input;

file.open(input.c_str());

if (!file)
{
cout << "File not found." << endl;
return 0;
}

while(a < 1) // To get you all the lines.
{
getline(file, input); // Saves the line in STRING.
if (input != prev_line)
{
prev_line=input;
cout<< input <<endl; // Prints our STRING.
}
counter++;

}

file.close();

return 0;
}
1
2
3
4
5
6
7
8
9
10
// initialize line number to 1

while( getline(file, input) ) // for each line in the file
{
    // print line number
    // print the line (input)
    // print a newline

    // increment line number
}
new Program, i get 0's in front of the lines. . .any tips?


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
ifstream file;
string input, String;
int a = 0;
string prev_line = "";
int counter = 0;

cout << "Enter filename" << endl;
cin >> input;

file.open(input.c_str());

if (!file)
{
cout << "File not found." << endl;
return 0;
}

a > 1;

while( getline(file, input)) // To get you all the lines.
{
cout << a << input << endl;
counter++;

}

file.close();

return 0;
}
Code tags, please.

What is the difference between
1
2
// print line number
// increment line number 

and
1
2
cout << a
counter++;
1
2
3
4
5
6
7
8
9
10
// int counter = 0;
int counter = 1;

while( getline(file, input) ) // To get you all the lines.
{
      // cout << a << input << endl;
      cout << counter << ". " << input << '\n' ;

      counter++;
}
You could also possibly use:
1
2
3
4
for(int line = 1; std::getline(file, input); ++line)
{
    std::cout << "Line " << line << " : " << input << std::endl;
}
Topic archived. No new replies allowed.