Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用C+;在文件中找到字符串后打印X行数+; 我试图在C++中实现一个函数,它在文件中搜索字符串,然后打印包含字符串和X后行的行。_C++_String_Lines_Paragraph - Fatal编程技术网

如何使用C+;在文件中找到字符串后打印X行数+; 我试图在C++中实现一个函数,它在文件中搜索字符串,然后打印包含字符串和X后行的行。

如何使用C+;在文件中找到字符串后打印X行数+; 我试图在C++中实现一个函数,它在文件中搜索字符串,然后打印包含字符串和X后行的行。,c++,string,lines,paragraph,C++,String,Lines,Paragraph,我有下面的代码用于查找字符串和打印行,但我无法让它使用字符串打印行下的行 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

我有下面的代码用于查找字符串和打印行,但我无法让它使用字符串打印行下的行

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();
}
Psuedo代码#2
我希望这足够清楚和详细。这就是我想用我的程序做的,但我找不到把它翻译成C++语句的方法。请告诉我是否有更好或更有效的方法来做我想做的事情。

既然你所拥有的对你有用,让我们先来看看。在这里,我只提取了相关部分,并添加了一点内容。我们需要做的就是在找到代码的情况下再打印四行,因此:

while (getline(myfile, line)) {
    if (line.find(codemp) != string::npos) {
        cout<<line<<endl;
        getline(myfile,line);
        found = true;
        for (int i=4; i; --i) {        <== new code begins
            cout << line << '\n';
            getline(myfile, line);
        }                              <== new code ends
    }
}
while(getline(myfile,line)){
if(line.find(codemp)!=string::npos){

cout我没有测试它,但应该可以:

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;
   }
}
while(getline(myfile,line))
{
if(line.find(codemp)!=string::npos)
{

谢谢你编辑我的帖子,我正试图这样做来修正伪代码间距,但你先得到了:D我尝试了你的解决方案,效果很好,但它没有打印出包含字符串的行后的第一行,我会找到解决方法:)这只是交换两行的问题。我已经更新了我的答案。我刚刚尝试了你的解决方案d它工作得很好,只需将var nbLines替换为5,因为这是我在代码中没有声明的,thx很多:)
while (getline(myfile, line)) {
    if (line.find(codemp) != string::npos) {
        cout<<line<<endl;
        getline(myfile,line);
        found = true;
        for (int i=4; i; --i) {        <== new code begins
            cout << line << '\n';
            getline(myfile, line);
        }                              <== new code ends
    }
}
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;
   }
}