【问题标题】:Deleting specific lines in a text file based one 1 variable删除基于一个 1 变量的文本文件中的特定行
【发布时间】:2016-08-20 20:56:44
【问题描述】:

目前有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:Ruby:xxxx:xxxx

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

【问题讨论】:

  • 拿出一张白纸和一支笔。用简单的英语写下做你想做的事情的合乎逻辑的逐步过程。完成后,只需将编写的过程直接翻译成 C++。如果你不能写下实现这一点的逻辑过程,那么你的问题与 C++ 无关。如果可以,但您在将其中的某些部分转换为代码时遇到问题,请用 specific 问题展示您目前已实现的内容。否则,stackoverflow.com 不是代码编写服务。
  • 我不是要求任何人编写它,我只是要求有关如何完成它的建议,我尝试使用 getline 创建一个新文件并复制旧文件的内容并排除该行已删除,但无法获得所有要删除的信息。我现在将重写此代码并编辑帖子,但我想可能会有更简单的东西。

标签: c++ arrays storage


【解决方案1】:

您想使用std::vector&lt;std::string&gt; 而不是声明字符串数组,例如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();
    }
}

【讨论】:

  • 为什么需要使用固定数组?
猜你喜欢
  • 2020-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-11
  • 1970-01-01
  • 1970-01-01
  • 2017-09-24
相关资源
最近更新 更多