尽管文件已被另一个程序更改,但fread不会读取更新的值 [英] fread isnt reading an updated value despite the file has been changed by another program
问题描述
有两个程序同时在处理我的特殊文件.他们有一个共同的开始:
#define TASK_POSITION 0x0100#include< stdio.h>#include< stdlib.h>#include< inttypes.h>int main(){FILE * control_file;unsigned int task_code;fpos_t task_position;control_file = fopen("/home/anonymous/.control","r +");fseek(control_file,TASK_POSITION,SEEK_SET);fgetpos(control_file,& task_position);
但是后来他们有了完全不同的代码
-
第一个程序可能会在此文件的TASK_POSITION偏移处更改一个值:
/* ...那里有很多代码... */task_code = 0xFEEDFACE;fsetpos(control_file,& task_position);fwrite(& task_code,4,1,control_file);fflush(control_file);睡眠(1);fclose(control_file);返回0;}
-
第二个程序反复读取与此文件相同偏移量的值:
<;> <代码>代表(;;){fsetpos(control_file,& task_position);fread(& task_code,4,1,control_file);如果(task_code == 0xFEEDFACE){/* ...做点很棒的事!... */}else {//调试后删除fprintf(stdout,尚未饿:P 0x%08x值已被读取... \ n");fflush(stdout);}睡眠(60);}//以防万一fclose(control_file);返回0;}
默认情况下,在TASK_POSITION偏移处存储了0x12345678值.
这是个问题:
在第一个程序启动并完成其工作之后,我可以在十六进制编辑器中看到一个特殊文件已成功更改.但是:由于某些我不知道的原因,第二程序的fread会继续读取相同的旧值-0x12345678!
尽管我找到了一个临时的解决方法,方法是在第二个程序的无限循环的每次迭代期间(在fread检查之间)关闭/打开此文件,但这似乎不是最好的解决方案!请告诉:如何强制fread实际从文件中重新读取新值,而不是仅仅返回先前读取的值(从其缓存"中?)?
C的标准I/O函数通常被缓冲.要禁用缓冲,您可以执行以下操作:
setvbuf(control_file,NULL,_IONBUF,0);
立即打开文件后,对文件执行任何I/O操作.
There are two programs that are working with my special file at the same time. They have a common beginning:
#define TASK_POSITION 0x0100
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
int main() {
FILE * control_file;
unsigned int task_code;
fpos_t task_position;
control_file = fopen("/home/anonymous/.control", "r+");
fseek(control_file, TASK_POSITION, SEEK_SET);
fgetpos(control_file, &task_position);
But later they have a very different code
1st program could change a value at TASK_POSITION offset of this file:
/* ... lots of code there ... */ task_code = 0xFEEDFACE; fsetpos(control_file, &task_position); fwrite(&task_code, 4, 1, control_file); fflush(control_file); sleep(1); fclose(control_file); return 0; }
2nd program is repeatedly freading a value at the same offset of this file:
for (;;) { fsetpos(control_file, &task_position); fread(&task_code, 4, 1, control_file); if (task_code == 0xFEEDFACE) { /* ... Do something awesome! ... */ } else { // remove after debugging fprintf(stdout, "not hungry yet :P 0x%08x value has been read... \n"); fflush(stdout); } sleep(60); } // just in case fclose(control_file); return 0; }
By default, there is 0x12345678 value stored at TASK_POSITION offset.
Here is a problem:
after the 1st program has been launched and completed its' work, I could see in a hex editor that a special file has been changed successfully. However: for some unknown to me reason, fread of 2nd program keeps reading the same old value - 0x12345678 !
Although I found a temporary workaround by fclosing/fopening this file during the each iteration of 2nd program's infinite cycle (between the fread checks) it does not look like the best possible solution! Please tell: how to force fread to actually re-read a new value from a file, instead of just returning a previously read value (from its' cache??) ?
C's standard I/O functions are typically buffered. To disable buffering, you can do:
setvbuf(control_file, NULL, _IONBUF, 0);
immediately after you open the file, before you perform any I/O on it.
这篇关于尽管文件已被另一个程序更改,但fread不会读取更新的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!