读取不断更新的文件(C ++) [英] Read a file that's constantly updated (C++)

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

问题描述

首先让我说我使用C ++大约3天了.

Let me start with saying that I'm around 3 days old in C++.

好吧,主要问题是,我有一个跨多行的文件,并且我试图重复打印一个特定的行,该行可能会因其他过程而任意更改.

Ok to the main question, I have a file that spans multiple lines, and I'm trying to print one specific line repeatedly, which is subject to change arbitrarily by some other process.

示例文件:

line0
line1
somevar: someval
line3
line4

我正在尝试打印中间行(以 somevar 开头的一行).我的第一次天真尝试是在以下位置进行操作:打开文件,循环浏览内容并打印确切的行,然后移至文件的开头.

I'm trying to print the middle line (one that starts with somevar). My first naive attempt was the following where I open the file, loop through the contents and print the exact line, then move to the beginning of the file.

#include <iostream>
#include <fstream>
#include <string>

int main (int argc, char *argv[])
{
    std::string file = "input.txt";
    std::ifstream io {file};

    if (!io){
        std::cerr << "Error opening file" <<std::endl;
        return EXIT_FAILURE;
    }

    std::string line;
    std::size_t pos;
    
    while (getline (io, line))
    {
        pos = line.find_first_of(' ');
        if (line.substr (0, pos) == "somevar:")
        {
            // someval is expected to be an integer
            std::cout << std::stoi( line.substr (pos) ) ) << std::endl;
            io.seekg (0, std::ios::beg);
        }
    }

    io.close();

    return EXIT_SUCCESS;
}

结果:每当文件更新时,程序都会退出.

Result : Whenever the file's updated, the program exits.

我开始认为我正在执行的IO实际上已被缓冲,因此更新文件不应像这样那样反映在我们现有的缓冲区中(这不是shell脚本).所以现在我想让我们在每次迭代中打开和关闭文件,这应该每次都有效地刷新缓冲区,我不知道最好的解决方案,但是我想测试一下理论.这是新的来源:

I came to think the fact that the IO I'm performing is actually buffered, therefore updating the file shouldn't reflect in our existing buffer just like that (this isn't shell scripting). So now I thought let's open and close the file on each iteration, which should effectively refresh the buffer every time, I know not the best solution, but I wanted to test the theory. Here's the new source :

#include <iostream>
#include <fstream>
#include <string>

int main (int argc, char *argv[])
{
    std::string proc_file = "input.txt";
    std::ifstream io;

    if (!io){
        std::cerr << "Error opening file" <<std::endl;
        return EXIT_FAILURE;
    }
    std::string line;
    std::size_t pos;
    
    while (io.open(proc_file, std::ios::in), io)
    {
        io.sync();
        getline (io, line); 
        pos = line.find_first_of(' ');
        // The line starting with "somevar:" is always going to be there.
        if (line.substr (0, pos) == "somevar:")
        {
            std::cout << std::stoi( line.substr (pos) ) ) << std::endl;
            io.close();
        }
    }

    io.close();

    return EXIT_SUCCESS;
}

结果:与以前相同.

实现我正在尝试的理想方法是什么?而且,每当有问题的文件被更新时,为什么程序会退出?谢谢(:

What would be the ideal way of achieving what I'm trying to? Also, why's the program exiting whenever the file in question is being updated? Thanks (:

我尝试读取的文件为"/proc/"+ std :: to_string(getpid())+"/io" ,该行是读取的字节数(以 read_bytes:开头).

The file I'm trying to read is "/proc/" + std::to_string( getpid() ) + "/io", and the line is the bytes read one (starts with read_bytes:).

推荐答案

我要读取的文件是一些/proc/1234/io

那是最重要的信息.

proc(5)中的文件是小型伪文件(有点像 pipe(7) -s),只能按顺序读取.

Files in proc(5) are small pseudo-files (a bit like pipe(7)-s) which can only be read in sequence.

该伪文件不会更新,而是会完全重新生成(由Linux 内核您可以研究其源代码)在每个 open(2)

That pseudo file is not updated, but entirely regenerated (by the Linux kernel whose source code you can study) at every open(2)

因此,您只需快速读取内存中的所有文件,并在读取后立即处理内存中的内容即可.

So you just read all the file quickly in memory, and process that content in memory once you have read it.

请参见此答案与一个非常相关的问题....使其适用于C ++

See this answer to a very related question.... Adapt it to C++

这篇关于读取不断更新的文件(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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