C++ |计算文件中两个特定点之间的行数

标签 c++ string file search counting

<分区>

我最近刚开始使用 C++,很想知道如何计算文件中特定点之间的行数。我已经可以成功地找到我想开始计数的第一个点,但我不知道如何进一步。

这是我到目前为止得到的:

ifstream fin;
string line;

char* search = "Key:";
char* search2 = "Color:";

// Open the file.
fin.open(filename);

// Check if it was successful in opening the file.
if (fin.fail() == true)
{
    return false;
}

// Read from the file and continue to read until the end of the file is reached.
while (getline(fin, line)) {

    if (line.find(search, 0) != string::npos) {
        //Start counting line ??
    }
}

如有任何帮助,我们将不胜感激。

最佳答案

使用您的代码,例如:

unsigned int count_lines=0;
unsigned int ignore_lines=0;

// Read from the file and continue to read until the end of the file is reached.
while (getline(fin, line)) {
    count_lines++;
    if (line.find(search, 0) != string::npos) {
        ignore_lines = count_lines;
    }
    if (line.find(search2, 0) != string::npos) {
        // The number of rows is count_lines - ignore_lines
    }
}

关于C++ |计算文件中两个特定点之间的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25157241/

相关文章:

objective-c - Objective C - 1 个 .h 文件需要 2 个 .m 文件?

c++ - 使用 RSA (PKCS 7) 分配/验证签名

c++ - 为什么 GDB 中的某些内存地址看起来比其他内存地址短?

php - 获取字符串中的前 2 个字符

javascript - 将对象中的值属性转换为 JavaScript 中的字符串

java - 在Java中添加元素到列表中

c++ - 是否可以将 WebRTC 浏览器转换为原生(C、C++ 或其他)?

c++ - 如何展平嵌套模板参数?

Java StringTokenizer.nextToken() 跳过空字段

从 C 中的 CSV 文件计算值的平均值