C++初级读本5版1.23从文件流中读取
C++ Primer 5 ed.1.23 Reading from a file stream
我通过学习C++初级读本第5版自学C++,我提出了练习1.23,其中规定:编写一个程序,读取几个事务,并计算每个ISBN发生的事务数量。
我曾想过使用变量来存储事务信息,但在实际场景中,这是不可扩展的。我做了一些研究,并试图通过Fstream标头读取文本文件。
我的问题是,当使用fstream时,我如何让它在文本文件中查找并返回该特定ISBN编号的条目数?
我的想法正确吗?有没有更好的方法来做到这一点?
像这样使用fstream只是一个基本想法:
#include <iostream>
#include <fstream>
#include <stdexcept>
std::ifstream file("file_name.txt");
std::string ISBN("99959");
if (!file)
throw std::runtime_error("Open file error");
std::string line; // Or whatever the data is in the file
int count = 0;
while (file)
{
std::getline(file, line);
if (line.find(ISBN) == std::string::npos)
++count;
}
相关文章:
- 正在将csv文件读取为双精度矢量
- 将从格式化文本文件读取的文本数据存储到链表
- C++ 读取文件读取文件不正确
- 使用对象文件读取三角形数据网格
- 如何减慢从 BVH 文件读取的 opengl 动画?
- 从文件读取,并写入函数C++中的数组
- 从对象C++中的文件读取内容时出现分段错误
- 从文件读取并通过矢量输出到控制台
- 尝试将字符串从文件读取到无符号字符向量中
- 如何将文本文件读取到二维数组中并以 c++ 打印
- 简化文件读取的函数
- 如何将逗号分隔的文件读取为 2D int 数组?
- 在 c++ 中将数据从文件读取到队列中
- 'std::wstring_convert'尽可能多地转换(从 UTF8 文件读取块)
- C++,从文件读取到结构,然后读取到向量(结构被推入向量太多次,而不仅仅是一次)
- 文件读取C++包括空格键
- 从文件读取时未将值推回矢量
- 从文件 C++ 读取时的无限循环
- 从同一文件读取和写入?
- 从.txt文件读取到C++中的双精度向量