如何删除字符串上方的行

How can I delete lines above string

我怎样才能删除字符串上方的一行?例如,我正在打开一个文本文件并对其进行编辑。我的程序的工作方式是用户输入教师姓名。 "Mr. John Doe" 然后它从搜索到的名称中删除接下来的 7 行。查找 "Courses" 和 "endInstructor" 之间的所有内容并将其删除,但剩下的是搜索名称上方的两个字符串,即 "loginpw123" 和 "startInstructor"。

现在我的问题是,在删除所有其他内容后,我将如何删除这两行,因为每个教授都不一样,并且列表中有大约 20 个配置文件。

代码如下:

int main()
{
    ifstream myfile("instructor.csv");
    ofstream myfile2("outfile.csv");
    string line;
    string dropInstructor;
    bool notFound = false;

    cout << "Enter Instructor to drop by name: ";
    getline(cin, dropInstructor);

    while (getline(myfile, line))
    {
        if (line != dropInstructor)
        {
            myfile2 << line << endl;
        }

        if (line == dropInstructor)
        {
            myfile2 << "";
            for (int i = 0; i < 7; i++)
            {
                getline(myfile, line);
                myfile2 << "";
            }
            getline(myfile, line);
            if (line == "Courses:")
            {
                myfile2 << "";
                while (getline(myfile, line))
                {
                    if (line == "endInstructor")
                    {
                        myfile2 << "";
                        break;
                    }
                    myfile2 << "tempp";
                }

                /*for (int i = 0; i < 4; i++)
                {
                    getline(myfile, line);
                    myfile2 << "";
                }*/
            }
        }
    }

    /*if (!notFound)
    {
        cout << "Not found" << endl;
        cin.get();
        // client ->sendToClient("Not Found");
    }*/

    myfile.close();
    myfile2.close();

    remove("instructor.csv");
    rename("outfile.csv", "instructor.csv");

}

文本文件:

loginpw123
startInstructor
Mr. John Doe
755 Teacher St
532 791 3761
johndoe@schoolname.ca
392817
03/02/1988
Male
777777
Courses:
Courseinfo1
Courseinfo2
Courseinfo3
Courseinfo4
Courseinfo5
endInstructor

在确定前两行是否属于要删除的教授之前,您正在将前两行复制到新文件中(为什么 startProfessor 上面的行不在 startProfessor 之间/endProfessor 而不是?)。需要先把第一行暂时存入内存,然后检查教授,如果不匹配则将所有与教授相关的行写入新文件。

尝试这样的事情:

int main()
{
    ifstream myfile("instructor.csv");
    ofstream myfile2("outfile.csv");

    string line, lineAboveProfessor;
    string dropInstructor;
    bool ignore = false, found = false;

    cout << "Enter Instructor to drop by name: ";
    getline(cin, dropInstructor);

    while (getline(myfile, line))
    {
        if (line == "startInstructor")
        {
            if (!getline(myfile, line))
                break;

            ignore = (line == dropProfessor);
            if (ignore)
            {
                found = true;
            }
            else
            {
                myfile2 << lineAboveProfessor << endl
                        << "startInstructor" << endl
                        << line << endl;
            }

            while (getline(myfile, line))
            {
                if (!ignore)
                    myfile2 << line << endl;

                if (line == "endProfessor")
                {
                    if (!ignore)
                        myfile2 << endl;
                    break;
                }
            }

            lineAboveProfessor = "";
        }
        else
        {
            lineAboveProfessor = line;
        }
    }

    /*if (!found)
    {
        cout << "Not found" << endl;
        cin.get();
        // client ->sendToClient("Not Found");
    }*/

    if (myfile1.eof() && myfile2.good())
    {
        myfile.close();
        myfile2.close();

        remove("instructor.csv");
        rename("outfile.csv", "instructor.csv");    
    }
    else
    {
        myfile.close();
        myfile2.close();

        remove("outfile.csv");
    }
}