So I have many log files that I need to write to. They are created when program begins, and they save to file when program closes.
I was wondering if it is better to do:
fopen() at start of program, then close the files when program ends – I would just write to the files when needed. Will anything (such as other file io) be slowed down with these files being still “open” ?
OR
I save what needs to be written into a buffer, and then open file, write from buffer, close file when program ends. I imagine this would be faster?
Well,
fopen(3) + fwrite(3) + fclose(3)is a buffered I/O package, so another layer of buffering on top of it might just slow things down.In any case, go for a simple and correct program. If it seems to run slowly, profile it, and then optimize based on evidence and not guesses.