C ++逐行读取文件的最快方法 [英] C++ fastest way to read file line by line

查看:60
本文介绍了C ++逐行读取文件的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不希望任何增强依赖或任何外部依赖.我可以逐行读取文件,然后分别处理每一行.但是,如果效果更好,我还可以将整个文件加载到内存中,然后逐行处理.

I don't want any boost dependency or anything external. I could read the file line by line, and process each line separately. But if it works better, I can also load the whole file in memory, and process it line by line afterwards.

什么是最好的方法?另外,最快的方法是什么,它们有何不同?

What's the best approach? Also, what is the fastest approach, how do they differ?

此外,这应同时适用于常规文本文件和通过终端通过管道传递文件.

Also, this should both work with a regular text file, and piping a file through terminal.

推荐答案

只需使用std :: getline .非常简单的解决方案:

Just use std::getline. Pretty straightforward solution:

std::ifstream file("filePath");
std::string line;
while (std::getline(file, line)) {
    // line contains the current line
}

这篇关于C ++逐行读取文件的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆