Search code examples
c++stringlinesparagraph

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


I'm trying to implement a function in C++ that searches for a string in a file, then prints the the line containing the string AND X subsequent lines.

I have the following code that works for finding the string and printing the line, but I can't get it to work to print the lines under the line with the string.

void repturno(void){

    system("cls");
    string codemp, line,output;
    bool found = false;
    ifstream myfile ("Pacientes.csv");

    //captures the string that i need to look for, in this case an employee code
    cout<<"\nBienvenida enfermera en turno, por favor introduzca su codigo"<<endl;
    cin.ignore();
    getline(cin,codemp);

    system("cls");

    /*reads the file and searches for the string, then prints the whole line, then searches again for
    the string and if finds it, will print the whole line again*/
    while (getline(myfile, line)) {
        if (line.find(codemp) != string::npos) {
                cout<<line<<endl;
                getline(myfile,line);
            found = true;
        }
    }
    //using this to check if the code was working, and verifying if the string was found or not :P
    if( found == false){
        cout <<"No se encontro la cadena"<< endl;
    } 

    system("pause");
    return menu();
}

Here are my problems:

  1. I can't get the cursor to go down to the next line and print it (need to do this 4 times), everything just gets messy and I end up with the same string and its line printed all over the screen many times.

  2. I can't save all the data pertinent to each employee in a single line (it would solve the problem in this function, but would create other problems); there's too much info for each employee, way more than the 80 characters allowed in the console and it messes up other parts of my program where I have to display the information on the screen and it looks really bad when i print it, even when trying to use gotoxy or '\t' for proper spacing. I had to save the data line by line, each category in a different line, so I can properly display it in other parts of my programs.

I came with 2 solutions in pseudo-code, but i don't know how to translate them into C++ sentences: NOTE: the search loop is already defined in the code above, so won't break it down step by step in the pseudo-code, will refer to it as SEARCH LOOP

Pseudo-code #1

START SEARCH LOOP
  SEARCH for the desired string(`codemp`) line by line
  IF string is found
    PRINTLINE containing the STRING
    MOVE CURSOR one line below
    PRINT the entire line
    MOVE CURSOR one line below
    PRINT the entire line
    MOVE CURSOR one line below
    PRINT the entire line
    MOVE CURSOR one line below
    PRINT the entire line
    Move CURSOR one line below
RESTART SEARCH LOOP in the current line
  IF NO matching string is found AND EOF
  END IF
END SEARCH LOOP
END PROGRAM

Psuedo-code #2

  START SEARCH LOOP
   SEARCH for the desired string(codemp) line by line
     IF string is found
       PRINTLINE containing the string
       PRINTING LOOP
         MOVE CURSOR one line below
         ASSIGN line to a temp_string
           IF temp_string = "\n\n" 
             //each employee is separated by 2 whitelines after its info
             DO NOTHING
           ELSE 
             PRINTLINE temp_string
             RESTART PRINTING LOOP
           END IF
       END PRINTING LOOP
 RESTART SEARCH LOOP
 IF NO string is found AND EOF
 END SEARCH LOOP
 END PROGRAM

I hope this is clear and detailed enough. That's what I want to do with my program, but I can't find a way to translate it into c++ sentences. Please let me know if there's a better, or more efficient way to do what I want to do.


Solution

  • I didn't test it but that should work :

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

    It iterate the file with a while and when it find the string it print "nbLines" other lines"