[問題] 請問fopen後,檔案被別人更動時,如何씠…
開發平台(Platform): Linux
額外使用到的函數庫(Library Used): non
問題(Question):
各位前輩好,
我現在需要透過檔案做狀態的輪詢,檔名叫msg,固定只存32bit的值。
我的程式用fopen打開msg後,每隔一段時間用fscanf去讀msg的值,
同時間會有另一個程式不定時更新msg的內容,
如果用vim去看msg,值都有更新
但是我的程式一值只讀到最初的值,
請問該怎麼樣才能抓到更新後的內容呢?
ps.因為輪詢的頻率很高,所以不能在每次讀值前才fopen,
避免頻繁的開關檔案。
謝謝各位前輩!
#include "stdio.h"
int main()
{
FILE *p1;
FILE *p2;
int i,s1,s2;
p1 = fopen("msg","r");
for (i=0;i<50;i++) { // the context of msg will be changed at random time
sleep(3); // just for test, let test time longer
fscanf(p1,"%d",&s1);
printf("%d\n",s1); // always the old value
p2=fope("msg","r");
fscanf(p2,"%d",&s2);
printf("%d\n",s2); // will be the newest value
fclose(p2);
}
fclose(p1);
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.66.243.96
※ 編輯: anniebu 來自: 61.66.243.96 (05/11 23:05)
→
05/11 23:57, , 1F
05/11 23:57, 1F
→
05/12 00:32, , 2F
05/12 00:32, 2F
→
05/12 00:57, , 3F
05/12 00:57, 3F
→
05/12 00:58, , 4F
05/12 00:58, 4F
→
05/12 01:06, , 5F
05/12 01:06, 5F
→
05/12 01:16, , 6F
05/12 01:16, 6F
→
05/12 09:07, , 7F
05/12 09:07, 7F
→
05/12 09:08, , 8F
05/12 09:08, 8F
→
05/12 09:09, , 9F
05/12 09:09, 9F
→
05/12 09:11, , 10F
05/12 09:11, 10F
推
05/12 12:09, , 11F
05/12 12:09, 11F
→
05/12 16:05, , 12F
05/12 16:05, 12F