2014-12-04 70 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

相關問題