Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C+;中读取由不同进程/线程修改的文件+;_C++_Multithreading_File_Ifstream - Fatal编程技术网

C++ 在C+;中读取由不同进程/线程修改的文件+;

C++ 在C+;中读取由不同进程/线程修改的文件+;,c++,multithreading,file,ifstream,C++,Multithreading,File,Ifstream,我想动态读取附加内容的文件。该文件由另一个线程写入。我正在使用ubuntu,并且已经看过inotify,但是我想尝试实现一种简单的方法,因为我只需要阅读附加的内容。我的尝试如下: ifstream logFile("path_to_file", ifstream::in); if(logFile.fail()) { cout << "file not found" << endl; exit(0); } while (true) { string

我想动态读取附加内容的文件。该文件由另一个线程写入。我正在使用ubuntu,并且已经看过inotify,但是我想尝试实现一种简单的方法,因为我只需要阅读附加的内容。我的尝试如下:

ifstream logFile("path_to_file", ifstream::in);
if(logFile.fail()) {
    cout << "file not found" << endl;
    exit(0);
}

while (true) {
    string line;
    getline(logFile, line);
    if( logFile.eof() ) {
        //wait for 2 seconds
        logFile.clear();
    }
    if(!line.empty())
        cout << line << endl;
ifstream日志文件(“路径到文件”,ifstream::in);
if(logFile.fail()){

无法复制谢谢,我已经使用了clear()方法来清除该问题中提到的错误标志,但仍然存在相同的问题。如果在另一个线程中进行写入,为什么不以某种方式同步它们?因为不是我在写入,我无法控制它。当然,同步就是答案。