c++ - Get the next line after some set characters -


i want read .txt file , after finding specific line...

=================== 

... want grab next line.

i think have use strcmp , fgets not sure how that.

simple enough std::getline:

const std::string wanted_line = "===================";  std::ifstream file(filename); std::string line; while (std::getline(file, line) && line != wanted_line){}  if (std::getline(file, line)) {/*read line after successfully*/} 

the loop goes long next line read , not 1 you're looking for. after that, read next one. if read failed in loop, 1 after loop fail.

if, reason beyond understanding, strcmp desired, can change minimally:

while (std::getline(file, line) && std::strcmp(line.c_str(), wanted_line) != 0){} 

for c solution, can same thing. did same thing on ideone stdin instead of file (easily changed) , limited lines 256 characters:

#include "stdio.h" #include "string.h"  int main(void) {     char line[256];     while (fgets(line, 256, stdin) && strcmp(line, "12345\n") != 0){}     if (fgets(line, 256, stdin)) {puts(line);}      return 0; } 

input:

abc
123
re me
12345
test line
hi

output:

test line


Comments