safe to open a file everyframe?

Started by
17 comments, last by Dwarf with Axe 21 years, 9 months ago
Hi, I was just wondering if it was safe (on performance & the processor) if I were to, every frame: open a file, extract information, close file open the file, write some stuff, close it again The files would be small, less than 10kb text files... THe reason I''m asking is because I want to buffer the input of a chat box into a text file. ~Dwarf
----------[Development Journal]
Advertisement
in short its not advicable, you can surely do this without a txt file, i was a little more awake i''d produce a sample. Perhaps someone else will, otherwise i''ll be back later
Why bother? Just keep it in memory. Accessing the harddrive is SLOW, and opening files isnt lightning fast either.
dwarf:if you do it everyframe it will be a performace killer. if you do it once a sec it might be acceptable, althrought you should avoid it like fire.

few tips:
1.leave file open if possible
2.read/write all data with one call, don''t perform many read/write operations as they are v.slow. It''s much faster to load everything into memory and work from there.
With best regards, Mirek Czerwiñski
If you *really need* to write to it every frame (more or less), why not keep it open all the time, and flush after every write (or after a few writes to increase performance). That way if your program crashes the file will still contain the data. Only unflushed data would be lost. There is certainly no reason to open it twice each frame (as your description suggests, once to read stuff and once to write. Just open it in modify mode allowing you to both read and write).

You will of course still lose a lot of performance if you write to a file every frame, but at least you won''t incur the overhead of opening and closing the file all the time.

The only valid reason I can think of to do anything like what you''re doing is that you''re afraid of losing data in case the game crashes (though a chat-log doesn''t sound like very critical data). If that''s the case, just make your game more stable .

Whatever the reason you should probably rethink the whole thing anyway. Keep your data in memory and save it as infrequently as you can. Preferably at times where speed isn''t critical, like when loading a new area/level, or when showing some menu, or waiting for some kind of non-realtime user interaction (e.g. during click-through dialog, or similar, if your game has anything like that). I''m sure you can find places in your game where it''s ''safe'' to save. Additionally you can have a time limit and/or size limit for the buffered data and save when one of the limits is reached (though your users are fairly likely to experience glitches and ''stuttering'' graphics when you do).
Bah! I guess all those years of good code practive let me slip on that one..


I never even thought about keeping it open the whole time... Is that a logical thing to do?

BTW, its not only chat stuff, but dynamic information thats going to be written to the file anyway... Just, accessing _it_ instead of a memory buffer seems easier...

Do the yay-or-nay on leaving it open sounds like a yay, aye?

~Jesse
----------[Development Journal]
You could of course keep it in memmory and only write it to a file every 15 minutes or when your program exit''s.
"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
quote:Original post by Dwarf with Axe
I never even thought about keeping it open the whole time... Is that a logical thing to do?

Sure.

Though, IMHO you should really reconsider the whole scheme. Why does it seem easier to write the stuff to a file rather than keep it in memory?
Yeah you, must really be doing something wrong to have to write data every frame... just keep it in memory and write when your done! I cant see whats so hard about this...

CEO Platoon Studios
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
If there is no reason to persist the information (save it outside the current game) then don''t bother making a text file, as the ppl above have mentioned it is a performance killer. If you want be able to look back on what has been said (like a chat history, saving the last 20 posts or whatever) I would use a vector or list of strings.

Free advice is often worth as much as it costs, remember that before flaming.

This topic is closed to new replies.

Advertisement