Home > database >  C how to print line and line number?
C how to print line and line number?

Time:12-05

I want to get the line number as a variable to use with my code, which is reading line by line and then returns the line. Then add LINE [LINENUMBER]: before each line

ifstream f(argv[1]);
string line;
string linenumber; // or use int instead if you wan't, doesn't matter. 

while (getline(f, line)) {
    cout << "LINE "   linenumber;   ": "   line << endl;
}

CodePudding user response:

What about this?

string line;
int line_number = 1;

while (getline(f, line)) {
  cout << "LINE [" << line_number    << "]: " << line << "\n";
}

CodePudding user response:

ifstream f(argv[1]);
string line;
int linenumber; 

while (getline(f, line)) {
    cout << "LINE "   linenumber;   ": "   line << endl;
    linenumber  ;
}

from the way your code looks, I assume you are trying to find how many lines are in some stream... there are much better and faster ways to do so if that is the case.

  •  Tags:  
  • c
  • Related