I need to read and write into a config file that has a few entries with simple format: key=value. When I write new value it can be shorter or longer than the value already in the file. At my understanding, to write into file I have to read and save all file context into a buffer, delete all file context, update the value, and then write everything back into a file from buffer.
I want to open file only once and then read and write into it. I want to avoid closing/opening file every time I need to write into it because it impedes performance.
Could someone suggest what is the best way to do it? Thank you!
I want to open file only once and then read and write into it. I want to avoid closing/opening file every time I need to write into it because it impedes performance.
I know how you feel, however, this sounds like premature optimization. Is it really impeding the performance of what you are trying to accomplish? If you really feel like you must read/write in one open then just use an fstream http://www.cplusplus.com/reference/iostream/fstream/.
I am reading & writing data (price quotes) from the remote server over tcp sockets, there is no performance number that would be satisfactory - the faster I do it the better. I need to keep count of incoming and outgoing messages and I need to store the counter into a file in case my application crashes.
This is what I am doing right now. I know how to read from file without file open/close, I do not know how to write into file (which is more critical than reading) without open/close. I certainly googled for it so this is the only solution I found. (Another solution is to delete original file, create another one and rename it, which is probably even slower.)
I'd appreciate any suggestion how to avoid opening/closing file every time I need to write into it.