目前的文本文件的特定行有4個不同的變量,保存的格式爲:刪除基於一個1可變
藝術家:標題:年:類
我目前打開文件並保存到相應的變量,我嘗試過使用getline,但無法讓它刪除所有相應的變量。
void deletesong(){
string oldfile;
ifstream openFile;
ofstream ofile;
string line;
string songnme;
string oartist[NUM], otitle[NUM], oyear[NUM], ocategory[NUM];
int counter;
bool finddvd;
//User enters filename, add .txt to make sure its saved as filename.txt
do{
cout << "Please enter the name of the file you would like to delete from: ";
cin >> oldfile;
oldfile += ".txt";
openFile.open(oldfile.c_str());
if(openFile.fail()){
cerr << "Check spelling of file name.\n";
error = true;
}
//Storing text from file into arrays
}while(error == true);
while(getline(openFile, oartist[counter], ':') && getline(openFile, otitle[counter], ':') &&
getline(openFile, oyear[counter], ':') && getline(openFile, ocategory[counter])){
counter++;
}
cout << "Enter the name of the song you would like to delete: ";
cin >> songname;
這是我用來檢索文件名的代碼,然後打開文件並將所有內容保存到數組中。
我需要我的代碼做的是對的例子是,如果有人想刪除紅寶石的一首歌曲的名字,它會在文本文件作爲
XXXX:紅寶石:XXXX:XXXX
我需要該程序刪除該行上的所有信息作爲其一條記錄。
取出一張空白紙和一支筆。用簡單的英語寫下一個合乎邏輯的循序漸進的過程,以便做你想做的事。完成後,只需將編寫的過程直接轉換爲C++。如果你不能寫下實現它的邏輯過程,那麼你的問題與C++無關。如果可以的話,但是在將代碼的某些部分轉換爲代碼時遇到問題,請使用***特定的***問題顯示迄今爲止已實現的內容。否則,stackoverflow.com不是一個代碼寫入服務。 –
我不是要求任何人寫它,我只是要求如何完成它的建議,我已經嘗試使用getline創建一個新文件並複製舊文件的內容並排除刪除的行,但不能獲取所有的信息將被刪除。我現在將重寫此代碼並編輯該帖子,但我認爲可能會更簡單一些。 – Thecube