Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

obtaining file line number

Status
Not open for further replies.

ernesth

Technical User
Jan 17, 2002
15
0
0
US
hello,
i am using an ifstream to read in characters from a text file into an array, and i need it to stop when it reaches a specific sequence of characters -- this i have done -- but, how do i get the line number in the file where it has stopped?

thanks in advance,
ernesth
 
Keep a counter and increment it every time '\n' is read. But be careful not to use something that skips whitespace through or you will never know if a newline was read. My advice would be to do something like this:

int counter = 0;
char buffer[32];
while(cin.getline(buffer, 32, '\n') {
// PROCESS THE LINE AND LOOK FOR STUFF

counter++;
}

This way counter will know the line number.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top