結果をファイルに書き込むプログラムがあり、そのファイルからリアルタイムで読み取りたいと考えています。これは通常のテキスト ファイルであり、外部プログラムは常に行全体を書き込みます。Linuxシステムでのみ実行する必要があります。
int last_read = 0;
int res;
FILE *file;
char *buf;
char *end = buf + MAXLINE - 1;
int c;
int fileSize;
char *dst;
while (external_programme_is_running()) {
   file = fopen(logfile, "r"); //without opening and closing it's not working
   fseek(file, 0, SEEK_END);
   fileSize = ftell(file);
   if (fileSize > last_read) {
      fseek(file, last_read, SEEK_SET);
      while (!feof(file)) {
        dst = buf;
        while ((c = fgetc(file)) != EOF && c != '\n' && dst < end)
            *dst++ = c;
        *dst = '\0';
        res = ((c == EOF && dst == buf) ? EOF : dst - buf);
         if (res != -1) {
            last_read = ftell(file);
            parse_result(buf)
         }
      }
  }
  fclose(file);
}
これは正しいアプローチですか?それとも、更新時刻を確認してからファイルを開いた方がよいでしょうか。ファイルが同時に変更された場合、読み取りがクラッシュする可能性はありますか?