2017-05-24 48 views
-1

你好我正在一個項目中,我有學生他們的數字姓氏和一切,我想選擇一個學生的名字,並刪除關於學生的整個信息在案例3:我的代碼:`我不能從文本文件中刪除單行C++

case 3:{ 


      int fNomer; 
      string ime; 
      string prezime; 
      string familiq; 
      string fakyltet; 
      string specialnost; 
      int grypa; 
      int kyrs; 
      string stud; 
      cout << "Stydenta koito iskate da iztriete"; 
      cin >> stud; 
      ifstream file; 
      ofstream outfile; 
      file.open("Student.txt"); 
      outfile.open("newM.txt"); 
      while (getline(file, line)) 
      { 
       if (line == deleteMovie){} 
       else { outfile << line << endl; } 
      } 
      outfile.close(); 
      file.close(); 
      outfile.close(); 
      file.close(); 
      remove("Students.txt"); 
      rename("newM.txt", "Students.txt"); 
      break; 
} 
+0

你應該正確地格式化你的代碼。例如,在你的C++教科書中。 –

+0

歡迎來到StackOverflow。 請參考[遊覽], 學習問好問題stackoverflow.com/help/how-to-ask, 做個[mcve]。一個MCVE應包括各種樣本輸入(說明所有方面)和期望的輸出。 如果您正在尋找調試代碼的幫助,請參閱https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ MCVE將提供關於例如deleteMovie的信息。這可能是失敗機制的一部分。 – Yunnosch

回答

0

的問題是在這裏:

while (getline(file, line)) 
{ 
    if (line == deleteMovie){} 
    else { outfile << line << endl; } 
} 

首先我們正確地格式化這段代碼:

while (getline(file, line)) 
{ 
    if (line == deleteMovie) 
    { 
    } 
    else 
    { 
     outfile << line << endl; 
    } 
} 

現在我們可以看到(以及我們可以,如果之前已經看到)您正在檢查對錯誤的變量deleteMovie代替stud

if (line == deleteMovie) 

應該

if (line == stud) 

您仍然需要添加錯誤檢查,例如,以處理無法打開文件的情況。