2016-08-20 53 views
-3

目前的文本文件的特定行有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

我需要該程序刪除該行上的所有信息作爲其一條記錄。

+3

取出一張空白紙和一支筆。用簡單的英語寫下一個合乎邏輯的循序漸進的過程,以便做你想做的事。完成後,只需將編寫的過程直接轉換爲C++。如果你不能寫下實現它的邏輯過程,那麼你的問題與C++無關。如果可以的話,但是在將代碼的某些部分轉換爲代碼時遇到問題,請使用***特定的***問題顯示迄今爲止已實現的內容。否則,stackoverflow.com不是一個代碼寫入服務。 –

+0

我不是要求任何人寫它,我只是要求如何完成它的建議,我已經嘗試使用getline創建一個新文件並複製舊文件的內容並排除刪除的行,但不能獲取所有的信息將被刪除。我現在將重寫此代碼並編輯該帖子,但我認爲可能會更簡單一些。 – Thecube

回答

2

您想要使用std::vector<std::string>而不是聲明字符串數組,例如string oartist[NUM]

此外,您可以將數據組織到一個結構中。例如:

struct record 
{ 
    std::string artist, title, year, category; 
}; 

使用std::vector::push_back添加數據。然後,您可以刪除位於矢量中間的記錄,也可以向矢量添加新數據。最後保存數據。例如:

std::fstream openFile(filename, std::ios::in); 
if (openFile.fail()) return; 

record rec; 
std::vector<record> songs; 

while (getline(openFile, rec.artist, ':') && 
    getline(openFile, rec.title, ':') && 
    getline(openFile, rec.year, ':') && 
    getline(openFile, rec.category)) 
    songs.push_back(rec); 

std::string title_remove = "title"; 

for (size_t i = 0; i < songs.size(); i++) 
{ 
    if (songs[i].title == title_remove) 
    { 
     songs.erase(songs.begin() + i); 
     break; 
    } 
} 

openFile.close(); 

openFile.open(filename, std::ios::out); 
for (size_t i = 0; i < songs.size(); i++) 
    openFile << songs[i].artist << ":" 
    << songs[i].title << ":" 
    << songs[i].year << ":" 
    << songs[i].category << "\n"; 

注意這個例子是使用fstream而不是使用ofstream。這是因爲它需要打開文件進行閱讀,然後關閉文件,然後用ios::out標誌再次打開以便寫入。


編輯,具有固定大小的數組做到這一點:

void deletedvd() 
{ 
    string oldfile; 
    ifstream openFile; 
    string line, dvdnme; 
    ofstream createFile; 
    string oartist[NUM], otitle[NUM], oyear[NUM], ocategory[NUM]; 
    string nartist[NUM], ntitle[NUM], nyear[NUM], ncategory[NUM]; 

    //********** 
    //initialize these values before using them 
    //********** 
    int counter = 0; 
    int j = 0; 
    bool error = false; 
    bool searc = false; 

    //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); 
     if (openFile.fail()) { 
      cerr << "Check spelling of file name.\n"; 
      error = true; 
     } 
    } while (error == true); 
    cout << "Enter the name of the dvd you would like to delete: "; 
    cin >> dvdnme; 
    while (getline(openFile, oartist[counter], ':') && getline(openFile, otitle[counter], ':') && 
     getline(openFile, oyear[counter], ':') && getline(openFile, ocategory[counter])) { 
     counter++; 
    } 

    //Loop for finding the deleted word, and overwriting it by shifting every to right of it left by 1 element 
    for (int i = 0; i < counter; i++) 
    { 
     if (dvdnme == otitle[i]) 
     { 
      //**** skip this item 
      searc = true; 
      continue; 
     } 
     ntitle[j] = otitle[i]; 
     nartist[j] = oartist[i]; 
     nyear[j] = oyear[i]; 
     ncategory[j] = ocategory[i]; 
     j++; 
    } 

    if (searc) 
    { 
     //********** 
     //close this file handle before opening a new handle 
     //********** 
     openFile.close(); 

     createFile.open(oldfile);//**** doesn't need the .c_str() with other std function 
     for (int i = 0; i < counter - 1; i++) { 
      createFile << ntitle[i] << ":" << nartist[i] << ":" << nyear[i] 
       << ":" << ncategory[i] << endl; 
     } 
     createFile.close(); 
    } 
} 
+0

爲什麼你需要使用固定陣列? –