how to print X amount of lines after finding a string in a file using C++ -


i'm trying implement function in c++ searches string in file, prints the line containing string , x subsequent lines.

i have following code works finding string , printing line, can't work print lines under line string.

void repturno(void){      system("cls");     string codemp, line,output;     bool found = false;     ifstream myfile ("pacientes.csv");      //captures string need for, in case employee code     cout<<"\nbienvenida enfermera en turno, por favor introduzca su codigo"<<endl;     cin.ignore();     getline(cin,codemp);      system("cls");      /*reads file , searches string, prints whole line, searches again     string , if finds it, print whole line again*/     while (getline(myfile, line)) {         if (line.find(codemp) != string::npos) {                 cout<<line<<endl;                 getline(myfile,line);             found = true;         }     }     //using check if code working, , verifying if string found or not :p     if( found == false){         cout <<"no se encontro la cadena"<< endl;     }       system("pause");     return menu(); } 

here problems:

  1. i can't cursor go down next line , print (need 4 times), gets messy , end same string , line printed on screen many times.

  2. i can't save data pertinent each employee in single line (it solve problem in function, create other problems); there's info each employee, way more 80 characters allowed in console , messes other parts of program have display information on screen , looks bad when print it, when trying use gotoxy or '\t' proper spacing. had save data line line, each category in different line, can display in other parts of programs.

i came 2 solutions in pseudo-code, don't know how translate them c++ sentences: note: search loop defined in code above, won't break down step step in pseudo-code, refer search loop

pseudo-code #1

start search loop   search desired string(`codemp`) line line   if string found     printline containing string     move cursor 1 line below     print entire line     move cursor 1 line below     print entire line     move cursor 1 line below     print entire line     move cursor 1 line below     print entire line     move cursor 1 line below restart search loop in current line   if no matching string found , eof   end if end search loop end program 

psuedo-code #2

  start search loop    search desired string(codemp) line line      if string found        printline containing string        printing loop          move cursor 1 line below          assign line temp_string            if temp_string = "\n\n"               //each employee separated 2 whitelines after info              nothing            else               printline temp_string              restart printing loop            end if        end printing loop  restart search loop  if no string found , eof  end search loop  end program 

i hope clear , detailed enough. that's want program, can't find way translate c++ sentences. please let me know if there's better, or more efficient way want do.

i didn't test should work :

while( getline(myfile,line) ) {    if( line.find( codemp ) != string::npos )    {       cout << line << endl;       for( int = 0; < nblines && getline( myfile, line ); ++i )          cout << line << endl;    } } 

it iterate file while , when find string print "nblines" other lines"


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -