如何更新和删除文本文件中特定的逗号分隔值? C ++

时间:2018-09-24 10:26:21

标签: c++ vector

我是一个初学者,正在尝试为学校项目制作清单程序,但是我的c ++知识很浅。假设我的txt文件中的数据存储以逗号分隔,我如何更新特定的数据字段/删除特定的数据字段?这是我到目前为止的代码。

    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    #include <vector>
    #include <sstream>
    using namespace std;

    string ItemID,
           description,
           name,
           category,
           manufacturer,
           SellingPrice,
           cost,
           StoreUnit,
           UnitSold,
           year,
           month,
           day;

    int choice=0;

    void mainmenu();
    void adddata();
    void savedata();
    void updatedata();
    float txtline(int);
    void searchdata();
    void showdata();

    vector<string> split(string strToSplit, char delimeter)
    {
        stringstream ss(strToSplit);
        string item;
        vector<std::string> splittedStrings;
        while (getline(ss, item, delimeter))
        {
           splittedStrings.push_back(item);
        }
        return splittedStrings;
    }

    vector <string> list;
    //-----------------------------------------------------------------------//

    int main()
    {
     mainmenu();

     while (choice==1)
     {
       adddata();
       mainmenu();
     }
     while (choice==2)
     {
       updatedata();
     }
     while (choice==2)
     {
       updatedata();
     }
     while (choice==5)
     {
      showdata();
      mainmenu();
     }
     while (choice==6)
     {
       return 0;
     }


    }

    void mainmenu()
    {
      cout << "___________________________________________________\n"
              "|                                                 |\n"
              "|       *** Inventory Management Program ***      |\n"
              "|              1 - Add New Record                 |\n"
              "|              2 - Update Data                    |\n"
              "|              3 - Delete Data field              |\n"
              "|              4 - Delete Record                  |\n"
              "|              5 - Show Record                    |\n"
              "|              6 - Exit                           |\n"
              "|_________________________________________________|\n"
              "        Please Select Your Next Choice: ";
      cin >> choice;
      cout << endl;
    }

    //-------------------------------------------------------------------------//

    void adddata()
    {
      cout << "*** Inventory Management Program *** \n"
           << endl
           << "Please Enter A New Record:\n";
      cout << endl;

      cout << "Item ID: ";
      cin.ignore();
      getline(cin,ItemID);

      cout << "Item Name: ";
      getline(cin,name);

      cout << "Item Description: ";
      getline(cin,description);

      cout << "Category: ";
      getline(cin,category);

      cout << "Manufacturer: ";
      getline(cin,manufacturer);

      cout << "Selling Price: RM";
      getline(cin,SellingPrice);

      cout << "Cost Price: RM";
      getline(cin,cost);

      cout << "Units in Store: ";
      getline(cin,StoreUnit);

      cout << "Units Sold: ";
      getline(cin,UnitSold);

      cout << "Year of Date First Introduced: ";
      getline(cin,year);

      cout << "Month of Date First Introduced: ";
      getline(cin,month);

      cout << "Day of Date First Introduced: ";
      getline(cin,day);

      savedata();

      cout << endl;
      cout << "All your data have been successfully stored.";
      cout << endl;
    }

    //------------------------------------------------------------------

    void savedata()
    {
      ofstream file;
      file.open("Milestone2.txt", fstream::app);

      file << ItemID << " , " << name << " , " << description << " , "
           << category << " , " << manufacturer << " , " << SellingPrice <<
           " , " << cost << " , " << StoreUnit << " , " << UnitSold <<
           " , " << year << " , " << month << " , " << day <<"\n";

      file.close();
    }

    //---------------------------------------------------------------------------
    void showdata()
    {
        string getcontent;
        ifstream openfile ("Milestone2.txt");
        if(openfile.is_open())
        {
            while(! openfile.eof())
            {
                getline(openfile, getcontent);
                cout << getcontent << endl;
            }
        }
    }

在输入值后,我的Milestone2.txt文件内部将是类似的内容。

1. 1,1,1,1,1,1,1,1,1,1
2. MU0001, Mr.Rabbit, Plush Toy, Toy, Kids Wonderland, 10.00, 5.00, 100, 33, 2017, 4, 25

我已经找到解决方案好几个小时了,但我不知道该怎么办...

1 个答案:

答案 0 :(得分:0)

您正在存储数据,但是密钥呢?当您顺序地接受来自用户的数据并将其存储在.txt文件中时,您只有一种选择来再次顺序地遍历数据以找出特定键的值,然后才可以对其进行更新。

我建议您将密钥与值一起存储,并在密钥名称前使用'$'或某些特殊字符以区分密钥与值。您将可以使用特定键进行搜索并更新其值。

我想回答一下,记住您是一名学生。如果您觉得很难实现它,那么仍然可以通过逗号(,)进行迭代以找到特定的字段并更新/删除相同的字段,但是下次当您迭代相同的数据时,它将与您的结构不匹配。我尚不完全了解您的项目范围,因此请考虑一下。