如何计算 C++ 中文件的行数?

发布于 2024-09-06 13:30:56 字数 65 浏览 1 评论 0原文

如何使用标准类 fstreamifstream 计算行数?

How can I count lines using the standard classes, fstream and ifstream?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

染柒℉ 2024-09-13 13:30:56

这个怎么样:-

  std::ifstream inFile("file"); 
  std::count(std::istreambuf_iterator<char>(inFile), 
             std::istreambuf_iterator<char>(), '\n');

How about this :-

  std::ifstream inFile("file"); 
  std::count(std::istreambuf_iterator<char>(inFile), 
             std::istreambuf_iterator<char>(), '\n');
枕头说它不想醒 2024-09-13 13:30:56

您逐行读取文件。
计算您阅读的行数。

You read the file line by line.
Count the number of lines you read.

捎一片雪花 2024-09-13 13:30:56

这是 Craig W. Wright 答案的正确版本:

int numLines = 0;
ifstream in("file.txt");
std::string unused;
while ( std::getline(in, unused) )
   ++numLines;

This is the correct version of Craig W. Wright's answer:

int numLines = 0;
ifstream in("file.txt");
std::string unused;
while ( std::getline(in, unused) )
   ++numLines;
何处潇湘 2024-09-13 13:30:56

之后的内核方法

@Abhay我完成的完整代码

#include <fstream>

std::size_t count_line(std::istream &is) {
    // skip when is not open or got bad
    if (!is || is.bad()) { return 0; }
    // save state
    auto state_backup = is.rdstate();
    // clear state
    is.clear();
    auto pos_backup = is.tellg();

    is.seekg(0);
    size_t line_cnt;
    size_t lf_cnt = std::count(std::istreambuf_iterator<char>(is), std::istreambuf_iterator<char>(), '\n');
    line_cnt = lf_cnt;
    // if the file is not end with '\n' , then line_cnt should plus 1
    // but we should check whether file empty firstly! or it will raise bug
    if (is.tellg() != 0) {
        is.unget();
        if (is.get() != '\n') { ++line_cnt; }
    }

    // recover state
    is.clear() ; // previous reading may set eofbit
    is.seekg(pos_backup);
    is.setstate(state_backup);

    return line_cnt;
}

:它不会更改原始文件流状态,并包括最后一行的“\n”缺失情况处理。

感谢 @masoomyf 指出我的错误,我太愚蠢了,无法弄清楚!

kernel methods following @Abhay

A complete code I've done :

#include <fstream>

std::size_t count_line(std::istream &is) {
    // skip when is not open or got bad
    if (!is || is.bad()) { return 0; }
    // save state
    auto state_backup = is.rdstate();
    // clear state
    is.clear();
    auto pos_backup = is.tellg();

    is.seekg(0);
    size_t line_cnt;
    size_t lf_cnt = std::count(std::istreambuf_iterator<char>(is), std::istreambuf_iterator<char>(), '\n');
    line_cnt = lf_cnt;
    // if the file is not end with '\n' , then line_cnt should plus 1
    // but we should check whether file empty firstly! or it will raise bug
    if (is.tellg() != 0) {
        is.unget();
        if (is.get() != '\n') { ++line_cnt; }
    }

    // recover state
    is.clear() ; // previous reading may set eofbit
    is.seekg(pos_backup);
    is.setstate(state_backup);

    return line_cnt;
}

it will not change the origin file stream state and including '\n'-miss situation processing for the last line.

Thanks @masoomyf for pointing my bug and I was too stupid to figure it out!

拥抱影子 2024-09-13 13:30:56
int aNumOfLines = 0;
ifstream aInputFile(iFileName); 

string aLineStr;
while (getline(aInputFile, aLineStr))
{
    if (!aLineStr.empty())
        aNumOfLines++;
}

return aNumOfLines;
int aNumOfLines = 0;
ifstream aInputFile(iFileName); 

string aLineStr;
while (getline(aInputFile, aLineStr))
{
    if (!aLineStr.empty())
        aNumOfLines++;
}

return aNumOfLines;
岁月蹉跎了容颜 2024-09-13 13:30:56

这对我有用:

  std::ifstream fin{"source.txt"};
  std::count(std::istream_iterator<char>(fin >> std::noskipws), {}, '\n');

This works for me:

  std::ifstream fin{"source.txt"};
  std::count(std::istream_iterator<char>(fin >> std::noskipws), {}, '\n');
倾城花音 2024-09-13 13:30:56

int numLines = 0;
ifstream in("file.txt");
//while ( ! in.eof() )
while ( in.good() )
{
   std::string line;
   std::getline(in, line);
   ++numLines;
}

存在一个问题,如果文件的最后一行不以换行符结尾,如何处理它。根据您在做什么,您可能想计算它,也可能不想计算。这段代码算了。

请参阅:http://www.cplusplus.com/reference/string/getline/


int numLines = 0;
ifstream in("file.txt");
//while ( ! in.eof() )
while ( in.good() )
{
   std::string line;
   std::getline(in, line);
   ++numLines;
}

There is a question of how you treat the very last line of the file if it does not end with a newline. Depending upon what you're doing you might want to count it and you might not. This code counts it.

See: http://www.cplusplus.com/reference/string/getline/

心是晴朗的。 2024-09-13 13:30:56

将文件大小除以每行的平均字符数!

Divide the file size by the average number of characters per line!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文