0

I have a program that need to write in a file every second.

I thought there is a too much overhead if I open and close the file each time. So I decided to keep open the file io stream.

But I don't want to rely on my unreliable hunch.

How can I find the better way if I should keep file stream open or close by every additional writing?

I want to check in CentOS environment.

ironsand
  • 5,205

2 Answers2

0

You aren't giving us a huge amount of detail, leaving out even what language you want to do this in, so my advice is to just try it.

Write two programs, one that opens/seeks-to-end-of-file/writes/closes every second. Write another program that just opens, writes, waits a second, writes again, waits a second, etc. See if either one works for you.

I suspect that even a shell script could do a write every second, but without more details, I just can't say for sure.

0

Definitely keep it open! The only reason to close it would be to keep it synced, but you can call sync by hand if you are scared of power outages. There is huge overhead if you are closing it all the time, because the buffer has to be flushed, and there are more write calls if you are writing every single chunk by hand. Not to mention seeking to the end of the file, modifying the timestamps in filesystem and so on...

orion
  • 12,502