如何从文本文件中删除一行数据

时间:2014-12-04 02:10:53

标签: c++ vector text-files

好的,我正在尝试创建一个程序,要求用户输入学生ID(即“12345”),它将运行一组文本文件并从所有文本文件中删除学生ID。首先,它检查“studnet.txt”文件,看看学生是否在那里。如果是这样,那么它将从该文件中删除该学生。如果学生在该文件中,这意味着他也在某些或所有其他txt文件中,因为其他txt文件是类。 (即 - biology.txt,chemistry.txt)。所以在学生档案中它看起来像这样

Students//this word is not actually in the file
100156
100167
100188
100177
123456
etc....

and the classes files look like this

biology
100167 98// the 98 represents a students mark in the course
100134 77
100165 54
100896 66
123456 88 

有多个类,所以这段代码的第二部分贯穿每个类,并且应该删除它。我可以从学生文本文件中删除学生,但是当我从课程中删除时我一直都会收到错误。它必须删除等级并保持适当的格式。此代码也是包含标头和其他类的更大代码组的一部分。有些变量可能会丢失。通常情况下,运行完整代码会显示一个菜单,当用户选择此选项时,它将调用此函数。

- - - - ----更新 这是我尝试运行时得到的错误

Debug Assertion失败!

程序: ... 1 \桌面\ Networking_Registrar \桌面\ Networking_Registrar.exe 文件:c:\ program files(x86)\ microsoft visual studio 10.0 \ vc \ include \ vector 行:932

表达式矢量下标超出范围

有关程序如何导致断言失败的更多信息,请参阅有关断言的可视化C ++文档。

(按“重试”调试应用程序)

void Student_Logger::reStudent (int removeStudent,vector<string> classlist){
    //int student2;
    bool checkinfile = true;
    //vector <int> rstudent;
    int variable;
    int count = 0;
    vector <int> myvector;
    ///fstream rStudentsfile;
    rStudentsfile.open("students.txt");
    while(rStudentsfile >> student2){

        rstudent.push_back(student2);
    }
    rStudentsfile.close();

    //ofstream file;
    file.open("students.txt");

    for (int i = 0; i < rstudent.size(); i++){

        if (rstudent[i] == removeStudent)
        {
            rstudent.erase(rstudent.begin()+i);
            checkinfile = true;
            break;
            //cout << rstudent[i] << endl;
        }
        else
        {
            checkinfile = false;
        }
    }
    if (checkinfile == false)
    {
        cout << "Student ID enterd is not registerd in the university." << endl;
    }
    cout << endl;
    for (int i = 0; i < rstudent.size(); i++){

        file << rstudent[i] << endl;
    }
    file.close();
//-----------------------------------------------------------------------this is the point where it checks all of the other files

    if (checkinfile == true)
    {
        string face;
        fstream openclass;
        for (int i = 0; i < classlist.size(); i++)
        {
            classes = classlist[i];
            openclass.open(classes.append(".txt",ios::app));

            while (!openclass.eof())
            {
                openclass >> variable >> face;
                myvector.push_back(variable);
                cout << variable << endl;

                if (variable == removeStudent)
                {
                    cout << "Hello" << endl;//lets me see if it chooses the correct line in the file

                    myvector.erase(myvector.begin()+count);

                }

                count++;
            }

            for(int i = 0; i < myvector[i]; i++)
            {
                openclass << myvector[i] << endl;

            }

            cout << endl;

            /*if (rstudent[i] == removeStudent)
            {
            rstudent.erase(rstudent.begin()+i);
            //cout << rstudent[i] << endl;
            }


            for (int i = 0; i < rstudent.size(); i++){

            openclass << rstudent[i] << endl;
            }*/


        openclass.close();
        }

    }


}

1 个答案:

答案 0 :(得分:1)

尝试更像这样的东西:

void Student_Logger::reStudent (int removeStudent, vector<string> &classlist)
{
    int student;
    vector<int> students;
    vector<string> lines;
    string line;
    bool found;

    ifstream ifile;
    ofstream ofile;

    ifile.open("students.txt");
    if (!ifile)
        return;

    found = false;
    while (getline(ifile, line))
    {
        istringstream iss(line);
        if (iss >> student)
        {
            if (student == removeStudent)
                found = true;
            else
                students.push_back(student);
        }
    }

    ifile.close();

    if (!found)
    {
        cout << "Student ID entered is not registered in the university." << endl;
        return;
    }

    /*
    TODO: to avoid corrupting your files, you should write new data to a separate
    temp file first, and then replace the original file with the temp file only if
    everything is successful.  If something goes wrong, you can simply delete the
    temp file and the original will not have been touched...
    */

    ofile.open("students.txt");
    if (!ofile)
        return;

    for(vector<int>::iterator i = students.begin(); i != students.end(); ++i)
    {
        ofile << *i << endl;
    }

    ofile.close();

    for (vector<string>:::iterator i = classlist.begin(); i != classlist.end(); ++i)
    {
        ifile.open(*i + ".txt");
        if (!ifile)
            continue;

        found = false;
        while (getline(ifile, line))
        {
            istringstream iss(line);
            if (iss >> student)
            {
                if (student == removeStudent)
                {
                    found = true;
                    continue;
                }
            }
            lines.push_back(line);
        }

        ifile.close();

        if (!found)
            continue;

        ofile.open(*i + ".txt");
        if (!ofile)
            continue;

        for(vector<string>::iterator j = lines.begin(); j != lines.end(); ++j)
        {
            ofile << *j << endl;
        }

        ofile.close();
    }
}