2016-12-30 73 views
0

我試圖刪除包含從文本文件 特定字線一條線,但它不工作刪除包含一個字

void deleteline() 
{ 
    string line, deletecontact; 
    cout << "Plase enter the contact (name or number) to delete:"; 
    cin >> deletecontact; 
    ifstream file; 
    ofstream outfile; 
    file.open("data.txt"); 
    outfile.open("newM.txt"); 
    while (getline(file, line)) { 
     if (line != deletecontact) { 
      outfile << line << endl; 
     } 
    } 
    outfile.close(); 
    file.close(); 
    remove("movieList.txt"); 
    rename("newM.txt", "data.txt"); 
} 

預先感謝您

+2

[它不工作(http://importblogkit.com/2015/07 /不,不工作/)? – wally

+0

'remove(「movieList.txt」);'不應該'remove(「data.txt」);'? –

回答

3

您只有在刪除線他們是平等的(即line != deleteContact)。如果你想刪除,正如你所說,只包含這個字線,你應該寫類似以下內容:

if (strstr(line.c_str(), deleteContact.c_str()) == nullptr) ... 
+2

另請參見std :: string :: find http://www.cplusplus.com/reference/string/string/find/ – Waxrat

+0

感謝但它仍然不刪除行 –

+0

使用'std :: search'來區分不區分大小寫搜索。 'strstr'更像是一個'C'功能。 –