从文本文件读取并保存到列表

Reading From Text File and Save to List

本文关键字:保存 列表 读取 文本 文件      更新时间:2023-10-16

Im 试图读取名为stock.txt的文件,该文件包含以下值:

ID、项目、颜色、尺寸、数量、价格

11,T-shirt,blue,XL,2,10.500000
12,Supreme,red,M,10,20.500000
13,BANG,red,M,10,20.500000

我想将每个项目存储在矢量列表中,我该怎么做?

int main() {
ifstream infile;
infile.open("Stock.txt");
string id; string title; string colour; string size; string quantity; string cost;
//If file open is successful
while(infile.good()){
getline(infile,id,',');
getline(infile,title,',');
getline(infile,colour,',');
getline(infile,size,',');
getline(infile,quantity,',');
getline(infile,cost,'n');        
}
infile.close();
}

然后,目标是实现一个搜索功能,我可以在其中搜索该列表中的特定项目(例如,通过使用 ID 或标题(

有两种常见的方法可以将元素添加到std::vector。一个是.push_back(),另一个是.emplace_back().push_back()将构造对象,然后将其复制到向量中。emplace_back()将直接在容器中构造对象。

我不完全确定你的意思

我想将每个项目存储在矢量列表中

但我会告诉你几种不同的方法。

字符串向量

由于看起来您拥有所有内容作为std::string,因此您可以继续制作字符串向量。但是,您有许多实际上不应该是字符串的数值。在下面的部分中,我将提供一些策略,以便能够将这些值维护为数字。

但是,在这里,如果您希望每个条目在向量中都有自己的元素,则您的代码将变为

int main() 
{
ifstream infile;
infile.open("Stock.txt");
string id, title, color, size, quantity, cost;
vector<string> all_data;  // Create vector
//If file open is successful
while(getline(infile, id, ','))
{
getline(infile, title,    ',');
getline(infile, color,    ',');
getline(infile, size,     ',');
getline(infile, quantity, ',');
getline(infile, cost,    'n');
// Add to vector
all_data.push_back(title);
all_data.push_back(color);
all_data.push_back(size);
all_data.push_back(quantity);
all_data.push_back(cost);
}
infile.close();
}

请注意,我确实更改了您的一些原始代码。

如果你想做一个大字符串作为一个元素,你也可以这样做:

int main() 
{
ifstream infile;
infile.open("Stock.txt");
string id, title, color, size, quantity, cost;
vector<string> all_data;  // Create vector
//If file open is successful
while(getline(infile, id, ','))
{
getline(infile, title,    ',');
getline(infile, color,    ',');
getline(infile, size,     ',');
getline(infile, quantity, ',');
getline(infile, cost,    'n');
// Make one big string
string new_element = 
title + ' ' + color + ' ' + ' ' + size + ' ' +
quantity + ' ' + cost;

// Add the new element to the vector
all_data.push_back(new_element);  
}
infile.close();
}

结构向量

可能是保留原始数据类型的最佳方法。使用此方法,将数据声明为结构,然后以这种方式提供所有内容。

struct Data
{
int id;
string title, color, size;
unsigned quantity;
double cost;
};
int main()
{
ifstream infile;
infile.open("Stock.txt");
string id, title, color, size, quantity, cost;
vector<Data> all_data; // Create vector of Data
//If file open is successful
while (infile.peek() != EOF)
{
int      new_id;
unsigned new_quantity;
double   new_cost;
getline(infile, id, ',');
getline(infile, title, ',');
getline(infile, color, ',');
getline(infile, size, ',');
getline(infile, quantity, ',');
getline(infile, cost, 'n');
// Convert some datatypes
new_id = stoi(id);
new_quantity = static_cast<unsigned>(stol(quantity));
new_cost = stod(cost);
// Add new element to vector
all_data.emplace_back(Data{ new_id, title, color, size, new_quantity, new_cost });
}
infile.close();
}

如果有什么我可以澄清的,请告诉我。

在数据结构和代码之间创建一些协同作用:

struct stock_item
{
std::string id;
std::string title;
std::string colour;
std::string size;
int quantity;
double cost;
};

一旦您能够从文件中的一行创建stock_item,您就可以轻松地随时随地/以您想要的方式存储和操作它:

std::vector<stock_item> some_vector;
stock_item some_stock_item;
// ...
some_vector.push_back(some_stock_item);
// ...
for (auto const& item : some_vector) {
std::cout << "Item #" << item.id << 'n';
}

所以你需要解析该文件:

auto stocks_from_stream(std::istream& in)
{
std::vector<stock_item> result;
std::string stock_line;
while(std::getline(in, stock_line)) {
auto item = stock_from_string(stock_line);
result.push_back(item);
}
return result;
}

然后,您需要定义此stock_from_string函数:

auto stock_from_string(std::string const& line)
{
stock_item result;
// parse your line here
return result;
}

瞧!