【发布时间】:2013-11-01 13:23:31
【问题描述】:
我正在实现一个轻量级应用程序,我必须经常打开并阅读 /proc/pid 或 tid/task/stat 详细信息。如果应用程序是多线程的,我必须阅读更多的统计文件。所以打开、阅读和关闭让我的监控应用程序真的很慢。是否有解决方案可以避免重复打开文件并且仍然能够读取更新的内容?
我进行了以下实验,但没有看到成功。我更改了“test.txt”中的数据,但没有读取新数据。是因为文件没有在内存中更新吗?当我修改并保存“test.txt”时会发生什么?
#include <stdio.h>
int main()
{
FILE * pFile;
char mystring [100];
pFile = fopen ("test.txt" , "r");
while(1){
if (pFile == NULL) perror ("Error opening file");
if ( fgets (mystring , 100 , pFile) != NULL ){
puts (mystring);
fseek ( pFile , 0 , SEEK_SET );
}
sleep(1);
}
fclose (pFile);
return 0;
}
【问题讨论】: