2014-12-04 125 views
-1

好吧,所以我想创建一个程序,要求用户输入一个学生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 

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

-----更新---- 这是错误我得到当我尝试和运行它

调试断言失败!

计划: ... 1 \桌面\ Networking_Registrar \桌面\ Networking_Registrar.exe 文件:C:\ Program Files文件(x86)的\微软的Visual Studio 10.0 \ VC \ \包括矢量 线:932

表达式向量下标超出范围

有关程序如何导致断言失败的更多信息,请参阅有关断言的Visual 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(); 
     } 

    } 


} 
+2

你什么错误?更新你的问题,包括这些。 – Dijkgraaf 2014-12-04 02:13:23

回答

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(); 
    } 
} 
+0

嘿谢谢!这工作完美! – Andre 2014-12-04 03:46:38