0

I'm trying to write a log file to my code but I have some restrictions and don't know how to surpass them.

My code runs for several days and loops every 1 minute. I want to write in the log file at the end of every loop, so the log file will have thousands of lines. So, my two main points about this are:

  1. I would like to be able to open and close the file at every loop (after I finish the operations, I open the file, write what I want and then close it). This way I can open the log file anytime to check how the code is going.
  2. Each line of the log file will have a different length depending of what happened in the loop. Since the file will have thousands of lines, I would like to be able to go to the next line without having to read all the previous existing lines.

I've tried to use the fseek function like this:

fseek(fp,-1,SEEK_END);

but had no success (I ended up writing over the already existing line). It's important to say that I'm writing this code in linux but would like it to be portable.

Everything I found here on other questions shows people reading the whole line and I don't need to read or store the existing lines. I just to want to open the file and write in a new line. Does anyone know how I can do this?

SergioAAV
  • 75
  • 3
  • 10
  • suggest looking at the functions exposed in `syslog.h` such as `openlog()`, `syslog()` and `closelog()` You can tell the `openlog()` exactly what file you want to use, so it doesn't have to be one of the system log files – user3629249 May 20 '19 at 01:58
  • @antti-haapala Could you unmark this as duplicate since I edited the question? – SergioAAV May 20 '19 at 02:38
  • 1
    @SergioAAV no. You're not supposed to mutate a question that has answers. In addition to that `/home/pi/Logs/Log_2019_05_19_23_24.txt` is 39 characters + terminating zero. How many characters in your array? – Antti Haapala -- Слава Україні May 20 '19 at 06:14
  • @AnttiHaapala sorry, I thought I could change. Won't happen again. Seems like the array size was wrong, but I had to end up using a little bit more than what I had counted. – SergioAAV May 20 '19 at 13:31
  • since you're targetting linux and Glibc, you could consider using `asprint` which allocates the memory for you! (just remember to `free` when no longer needed) – Antti Haapala -- Слава Україні May 20 '19 at 13:33

1 Answers1

0

Open the file in append mode ("a" in fopen). That way all writes will go to the end of the file; no seeking required.

Also, there is no point in opening/closing the same file repeatedly. Just open the file once, before the loop starts. Keeping a file open does not prevent others from reading it. If you're concerned with delays caused by buffering, you can just fflush() the handle after every line of output.

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • Thanks for reminding me of the "a" option. It's been a long time that I don't use files and had totally forgotten that this option existed. I edited the question with my code and what's happening. – SergioAAV May 20 '19 at 02:40