如何更新和删除文本文件中的特定逗号分隔值?C++
How to update and delete specific comma separated values in text file? C++
我是一个初学者,试图为学校项目制作一个库存程序,但我的 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文件中时,您只有一个选项可以再次按顺序浏览数据以找出特定键的值,然后只有您可以更新它。
我建议您将键与值一起存储,并在键名前使用"$"或一些特殊字符来区分键和值。您将能够使用特定键进行搜索并更新其值。
我试图回答,记住你是一名学生。如果您觉得难以实现它,那么您仍然可以遍历逗号 (,( 以找出特定字段并更新/删除相同的字段,但下次当您迭代相同的数据时,它将与您的结构不匹配。我不完全了解您的项目范围,所以也想提一下您。
相关文章:
- 如何将 txt 文件中的行分隔为组件C++
- 无法通过空白将文本文件行分隔为矢量
- 如何在C++中读取空格分隔的输入 当我们不知道输入的数量时
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 绑定派生类方法C++从实例范围之外的分隔 std::function 变量调用
- 在C++的两个字符串中连接以逗号分隔的唯一值
- 如何逐行读取文件,每行中的内容都用空格分隔并将其写入新文件中
- 用于存储由空格分隔的字符串的 C++/C 数据结构
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- 如何在 C++ 中将文件中的逗号分隔数字读取到数组中?
- "Warning: Comma within array index expression"但逗号分隔函数参数
- C++逗号分隔的输入数组代码过早退出
- 如何分隔字符串并将标记传递给方法
- 分隔整数 (C++) 的数字
- 野牛/yacc 解析器在不被空格分隔时跳过 grammer - "unexpected $end"
- 读取制表符分隔的文件时出现问题 C++
- 通过分隔符分隔包含 UTF-16 BE 文本的uint8_t数组
- 如何更新和删除文本文件中的特定逗号分隔值?C++
- 如何将逗号分隔的文件读取为 2D int 数组?
- 读取带有分隔C++的文件