基于一个1变量删除文本文件中的特定行

时间:2016-08-20 20:56:45

标签: c++ arrays storage

目前有4个不同的变量,保存格式为:

歌手:标题:年:类

我正在打开文件并保存到相应的变量中,我尝试使用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;

这是我用来检索文件名的代码,然后打开文件并将所有内容保存到数组中。

我需要我的代码做的是例如,如果有人想要删除Ruby的歌曲名称,它将在文本文件中作为

XXXX:红宝石:XXXX:XXXX

我需要程序删除该行上的所有信息作为其中一条记录。

1 个答案:

答案 0 :(得分: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();
    }
}