从C++文件中读取,划分输入

Reading from a File in C++, Dividing the Input

本文关键字:划分 输入 读取 C++ 文件      更新时间:2023-10-16
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

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

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

然后

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

但是我不知道如何写第一个条件。我的书只教我们如何在文件 I/O 中使用 get 和 getline...显然是这样的

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

由于操作数类型而不起作用。对不起,如果我的问题措辞很差,这只是我想到的处理方式。谢谢。

您可以使用

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

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

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