gpt4 book ai didi

c++ - 从 C++ 文件中读取带分隔符的 int 和 string

转载 作者:搜寻专家 更新时间:2023-10-31 01:06:08 41 4
gpt4 key购买 nike

我正在尝试逐行读取文件中的内容。基本上每一行都会有 ID 号、价格、数量和书名。我知道在每次输入后,我必须将分隔符标志放在那里以停止读取,但我不知道如何。我尝试使用 getline(),但是 getline() 只能接受一个字符串,而 id #、价格和数量。有人愿意告诉我如何阅读文件吗?谢谢,这就是我目前所拥有的。

例子:该文件应该包含很多行,每行的格式如下:- 第一个数字是身份证号码- 第二个是价格- 第三个是数量- 最后一个是书名

123,19.99,5,夏天很有趣

void ReadFromFile(int ids[], int prices[], int qty[], string titles[], int& count)
{
ifstream infile("tomatoes.txt");
if(!infile.is_open())
cout << "File does not exists" << endl;
else
{
while(!infile.eof())
{
infile >> ids[count];
infile >> prices[count];
infile >> qty[count];
getline(infile, titles[count]);
if(!infile.eof())
count++;
}
}
}

最佳答案

所以,我们又来了,另一个解析问题。

基本的读取循环。
读取文件不使用 eof 方法来确定文件结尾。您可以在 StackOverflow 上搜索“c++ read eof while”以获取一些示例。

std::string textline;
while (getline(infile, textline)
{
}

上面的循环将一次读取一个文本行,直到读取失败为止,这通常是文件的末尾。

解析文本
我们可以通过将其视为输入流来解析文本字符串。这涉及使用 std::istringstreamgetlinegetline 有一个重载 版本,它将读取文本直到找到指定的分隔符。在您的例子中,这个分隔符是一个逗号。

std::string textline;
while (getline(infile, textline)
{
string comma_string;
std::istringstream text_stream(textline);
text_stream >> id;
getline(text_stream, comma_string, ','); // Read characters after number until comma.
text_stream >> price;
getline(text_stream, comma_string, ','); // Read characters after number until comma.
text_stream >> quantity;
getline(text_stream, comma_string, ','); // Read characters after number until comma.
// Read the remaining text, until end of line, as the title.
getline(text_stream, title, '\n');
}

存储记录
输入循环已扩展为在字段中读取。它们应该存储在一个对象中。

std::string textline;
while (getline(infile, textline)
{
string comma_string;
std::istringstream text_stream(textline);
text_stream >> id;
getline(text_stream, comma_string, ','); // Read characters after number until comma.
text_stream >> price;
getline(text_stream, comma_string, ','); // Read characters after number until comma.
text_stream >> quantity;
getline(text_stream, comma_string, ','); // Read characters after number until comma.

// Read the remaining text, until end of line, as the title.
getline(text_stream, title, '\n');

// Put the file data fields into an object.
book.id = id;
book.price = price;
book.quantity = quantity;
book.title = title;
}

将数据附加到容器

std::string textline;
std::vector<Book> catalog;
while (getline(infile, textline)
{
string comma_string;
std::istringstream text_stream(textline);
text_stream >> id;
getline(text_stream, comma_string, ','); // Read characters after number until comma.
text_stream >> price;
getline(text_stream, comma_string, ','); // Read characters after number until comma.
text_stream >> quantity;
getline(text_stream, comma_string, ','); // Read characters after number until comma.

// Read the remaining text, until end of line, as the title.
getline(text_stream, title, '\n');

// Put the file data fields into an object.
book.id = id;
book.price = price;
book.quantity = quantity;
book.title = title;

// Append the book to the catalog
catalog.push_back(book);
}

结论
你不应该期望人们给你正是你想要的,但你应该能够获取代码片段并将它们与你的代码片段进行比较,看看你是否能让它们工作。

关于c++ - 从 C++ 文件中读取带分隔符的 int 和 string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21418506/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com