2016-12-30 85 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'功能。 –