[英]Show on Console the dynamic changes of a .txt file in C++
Here is my requirement: " I need to show to the user on a Console window, the output of a .txt file which will be modified continuously by a Third party application " 这是我的要求:“ 我需要在控制台窗口上向用户显示.txt文件的输出,该文件将被第三方应用程序连续修改 ”
I have this code from cplusplus.com : 我有cplusplus.com的这段代码:
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("test_results.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Do I need any sort of time counter to read that file at certain regular intervals of time? 我是否需要某种类型的计时器,以一定的固定时间间隔读取该文件? Guide me, thanks in advance...
指导我,在此先感谢...
There are two approaches. 有两种方法。
Platform-dependent 平台相关
Use API, like inotify
in Linux 使用API,例如Linux中的
inotify
Platform-independent 平台无关
Query fs about file modifications, for example using boost::filesystem
http://www.boost.org/doc/libs/1_52_0/libs/filesystem/doc/reference.html#last_write_time 查询有关文件修改的文件,例如使用
boost::filesystem
http://www.boost.org/doc/libs/1_52_0/libs/filesystem/doc/reference.html#last_write_time
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.