-1

While working with joy, it stores JSON data in a JSON file. Questions: I have a c file which reads joy JSON file. But it cannot read the current file in which joy is inserting.

  1. use tail to read live data when joy is inserting its data in a JSON file.
  2. check the last line read.

Help me on that, a sample in c which reads live data of a file.

I have a program which is writing results to a file and I would like to read in real-time from that file. It is a normal text file and external program always write a whole line. I need to run it just on a Linux system.

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);
}

Is this a correct approach? Or would it be better to check the modification time and then open the file? Is is possible that reading would crash in case that the file would be modified at the very same time?

user438383
  • 4,338
  • 6
  • 23
  • 35
SWAT
  • 1
  • 1
  • Please [edit] your question and show your code. What exactly means "*it cannot read the current file in which joy is inserting*"? – Bodo May 18 '22 at 09:01
  • Did you take a look on implementation of `tail` command? What environment are you using? Windows, Linux, embedded, ...? – Gerhardh May 18 '22 at 09:08
  • @Bodo It means that when joy stores all its file in a directory, i am reading files in directory, it can read all json files until the current file in which joy is inserting its data. – SWAT May 18 '22 at 09:12
  • @Gerhardh, i have seen but cannot understand it, Basically I want to ask ->Read from a file which is being actively written to using tail in c language – SWAT May 18 '22 at 09:16
  • I understand. That is exactly what the `tail` command does. If you are using Linux the code should provide some hints how you can open the file while it is written by other program. As a precheck: Are you able to show the content with `tail` program? – Gerhardh May 18 '22 at 09:23
  • Please also state what your problem is. What does `cannot read` mean? Does opening fail? What error? Can you open but don't see new content? How do you try it, to start with... Show your code together with the failing result. – Gerhardh May 18 '22 at 09:24
  • @SWAT **Please [edit] your question and add all requested information or clarification to the question** instead of using comments for the purpose. Your answer "*it can read all json files until the current file in which joy is inserting its data*" does not clarify what actually happens. Do you get an error? Or a behavior other than you would expect? What is your OS? Windows does not allow certain file operations for files that are held open (or locked?) by a different process. – Bodo May 18 '22 at 09:53
  • I just need that, can any one write a code in c using tail and it do same work. – SWAT May 18 '22 at 10:03
  • You did not yet tell us how your approach fails to work. You have a few bugs: 1) You don't check I/O functions for success. 2) See [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Gerhardh May 18 '22 at 10:27

0 Answers0