C++文件I/O选项卡分隔的数据
C++ File I/O Tab separated data
我有一个数字如下的文本文件:num1制表符num2制表符……num22换行符。。.
我想阅读num1检查,看看它是否等于3,如果是,将整行复制到一个新文件中。做这件事最快的方法是什么?这个文件相当大,80Mb+。此外,num 1是重复的,即它以0.001的步长从0变为3。所以我只需要每隔这么多步就读一次。我不知道如何告诉计算机先验地跳过x行?
谢谢。
如果您已经说过运行时性能不是主要问题,那么以下内容是清晰简洁的:
#include <string>
#include <fstream>
void foo(std::string const& in_fn, std::string const& out_fn)
{
std::ifstream is(in_fn);
std::ofstream os(out_fn);
std::string line;
while (std::getline(is, line))
if (line.size() && std::stoi(line) == 3)
os << line << 'n';
}
(假定支持C++11;为简洁起见,省略了错误处理。)
伪代码可以如下所示:
while (not eof) {
fgets(...);
find TAB symbol or end of line
get string between two marks
cleain it from spaces and other unnecessary symbols
float fval = atof(...);
if (fval == 3) {
write the string into new file
}
}
相关文章:
- 用于存储由空格分隔的字符串的 C++/C 数据结构
- 嵌套结构,从由空行分隔的文件中读取数据
- 使用 range-v3 读取包含逗号分隔数据的行
- C 程序要读取包含两个双重值的Excel数据的单列,这些值由逗号分隔并存储在自己的数组中
- C++文件I/O选项卡分隔的数据
- 要存储到数据结构中的制表符分隔的文件数据
- C++读取元数据文件,并处理由空白和:混合分隔的字段
- 如何从用|分隔的txt文件中提取数据
- 当数据包含空格时,如何将制表符分隔文件的内容加载到C++字符串的 2D 向量中
- 对于二进制数据,哪个性能更高:将整个对象写入文件或分隔变量(无对象)
- 在c++中分析不同类型的逗号分隔数据
- 从制表符分隔的文件中读取2D数据并存储在矢量C++中
- 从逗号分隔的文本文件中创建有意义的数据向量的最佳方法是什么?
- 从字符串中读取多个数据,使用sstream以字符分隔
- 如何在c++中读取以空格分隔的十六进制字节数据
- 如何从C++中字段由'|'字符分隔的行中提取数据?
- 写一个逗号分隔的字符串属性到HDF5数据集c++
- 如何在c++中读取以空格分隔的数据并将其保存到数组中,然后以其他顺序写入新的文本文件
- 用空格数据读取空格分隔的数据
- 解析制表符分隔的数据