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

How to update and delete specific comma separated values in text file? C++

本文关键字:分隔 C++ 文件 何更新 更新 文本 删除      更新时间:2023-10-16

我是一个初学者,试图为学校项目制作一个库存程序,但我的 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

几个小时以来,我一直在寻找解决方案,但我无法弄清楚如何...

您正在存储数据,但是密钥呢?当您按顺序接受来自用户的数据并将其存储在.txt文件中时,您只有一个选项可以再次按顺序浏览数据以找出特定键的值,然后只有您可以更新它。

我建议您将键与值一起存储,并在键名前使用"$"或一些特殊字符来区分键和值。您将能够使用特定键进行搜索并更新其值。

我试图回答,记住你是一名学生。如果您觉得难以实现它,那么您仍然可以遍历逗号 (,( 以找出特定字段并更新/删除相同的字段,但下次当您迭代相同的数据时,它将与您的结构不匹配。我不完全了解您的项目范围,所以也想提一下您。