Skip to main content
GameDev.net gamedev.net
🔒 Locked

Help with files!!!

Started by mits Feb 15, 2003 at 6:29 AM 1 replies 300+ views
Original Post
mits
mits
I have a class declaration: class Item { public: int id; Item(int n){id=n}; }; I use the fwrite() to write e.g 3 instances of this class in a file.If I want to add another one instance , what to I do?I try to go to end of file with fseek(fp,0,SEEK_END) and then to add the instance with fwrite() but something goes wrong.Can anyone help me or propose another way to do it? Thanks in advance.
CProgrammer
CProgrammer
If you copy in one go use multiple fprintf calls which add the text after the existing one. If you add at a later time read in the file data with fread then copy it back in with the new data.

-CProgrammer
My Website
Fruny
Fruny
Did you remember to open the file in "w+" or "a+" mode, so that it is not erased first ? Note that ''a'' automatically moves you to the end (append).


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.