2016-03-30 49 views
1

我在電話簿項目中刪除項目的部分我有問題;這裏是我的刪除部分代碼:重命名並刪除cpp中的文件

 void pbook::del(){ 
    cout<<"Enter Id(Be sure to Enter with #) or a full info of a Contact to Delete:"<<endl; 
    string ds,line,deltemp; 
    cin>>ds; 
    ifstream pb("pb.gh"); 
    while(getline(pb,line)){ 
    if (line.find(ds) != string::npos) {  
    deltemp=line; 
    continue; 
     } 
     else{ 
      fstream pbtemp("pbtemp.gh",ios::app); 
      pbtemp<<line<<endl; 
     } 

    } 
    cout<<deltemp<<" was successfully deleted !"; 
    }  

我試圖寫的所有聯繫人,除了後刪除通用電話簿文件中刪除一個bptemp.gh但remove("pb.gh")不會做任何事情!我認爲將ifstream pb("pb.gh");更改爲fstream pb("pb.gh");會有所幫助,但在做完之後,「pbtemp.gh」將不會生成! 請幫我刪除「pb.gh」並將「pbtemp.gh」重命名爲「pb.gh」。 Regards

+0

我的猜測是,你試圖在'pb'仍然打開的時候刪除文件。首先關閉流。 –

+2

C++目前沒有提供任何標準的手段來刪除文件。它應該在下一個版本中可用:http://en.cppreference.com/w/cpp/filesystem – NathanOliver

+0

@NathanOliver那麼'remove()'怎麼辦? –

回答

0

首先,remove(const char *)返回一個錯誤代碼。檢查它,你會知道發生了什麼。其次,爲什麼不使用C++庫而不是c函數?看看boost :: filesystem(也許它會包含在C++ 16中)