gpt4 book ai didi

c++ - 在 C++ 中读取文件,分割输入

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:59:33 28 4
gpt4 key购买 nike

Number of Copies
Movie Title
Movie Actor
Movie Actor
Movie Director
Movie Producer
Movie Production Co.
Number of Copies
Movie Title
Movie Actor
Movie Actor
Movie Director
Movie Producer
Movie Production Co.

Customer1 ID
Customer2 ID
Customer3 ID
Customer4 ID

我有一个关于 C++ 的文件 I/O 问题。如果我有一个看起来像这样的输入文件,我可以使用什么命令停止将 getlines() 插入到顶部的变量中,以便开始将 getlines() 插入到底部的变量中?在伪代码中,我可以想象它会是这样的

while (the next line isn't "\n"), keep adding to the movie database.

然后

while (!fin.eof()), start adding to the customer database

但是我不知道第一个 while 条件怎么写。我的书只教我们如何使用 get 和 getline 进行文件 I/O...显然类似

while (fin.get() != "\n")

由于操作数类型而不起作用。抱歉,如果我的问题措辞不佳,这就是我想到的处理方式。谢谢。

最佳答案

您可以使用 std::getline(std::istream&, std::string&)获取一行并将其保存在 std::string 中. std::string 提供了一个方法 empty检查字符串是否为空。

std::getline 不会提取分隔符,因此您应该很容易检查一行是否为空。

(提取提示:为了从您的行中提取子字符串,您可以使用 std::istringstream 。)

关于c++ - 在 C++ 中读取文件,分割输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11301288/

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