从C++文件中读取,划分输入
Reading from a File in C++, Dividing the Input
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
。
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- C++如何通过用户输入删除列表元素
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 用户定义函数中的指针和输入
- 如何在C++中检查2D数组中负值的输入验证
- 如何只允许用户输入正整数
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 输入中的字符串数未知(以字母表示)
- 从C++文件中读取,划分输入
- 解析格式化输入时划分单词时出现问题