Friday, July 31, 2009

Creating Files In C++?

Hello, I am wondering in most they have their own file types (such as .sav for a game save and .esm for a Elder Scrolls file) Just wondering how do they create, acess and delete them using C++? Can anyone give me an explaination, point me towards a tutorial or tell me a book i could get with this in it.





- Cheers, Daniel

Creating Files In C++?
To read or write text files in C++, you usually use stream objects of the standard classes "ifstream" and "ofstream." Those classes also have open and close functions and the appropriate overloaded stream operators. There is nothing really special about the different types of file name extensions, although it helps to choose a unique one that means something in the context (like an abbreviation). To read or write binary data in C++ (for example, from data structures), there are other functions you can use called "read" and "write." They take character buffers as their arguments, but you can cast any buffer to a buffer of characters so that doesn't matter. This appears to be a good place for you to start learning about it: http://www.angelfire.com/country/aldev0/...


This is another tutorial-like reference related to the subject:


http://www.developer.com/net/cplus/artic...


Another handy reference (but not a tutorial) is http://www.cppreference.com/





There's nothing particularly special about writing to files per se. The main difference between writing to files and to the console is that you have to decide on a good format for your files if you intend to read them later on. Good luck!
Reply:These are input output operations that are available in every language.


I would rather advise you to move with C# than C++ if your developing for the windows environment.
Reply:If you've ever tried opening a data file (such as a saved game file) in notepad you've probably noticed that it just looks like a bunch of gibberish. That's because when you open a file in Notepad, it has certain assumptions that it makes about the file that aren't true.





Almost all text files are encoded with 7-bit data, since 7 bits can sufficiently hold all the letters, numbers, punctuation and control characters you need.





Data files are almost always encoded in 8-bit data. So when notepad starts reading it, it looks like gibberish.





Files in C++ are handled almost exactly like input/output from the keyboard and to the screen. Except instead of using the devices, it uses file streams.





To open a file steam, you se the fstream.open() function. The parameters that you pass that function determine whether you will be reading to it, writing to it, or both.





From there you read in data just like you would read in data from the keyboard. Except the data might be more optimized than text.





You coud fit 8 different flags into a single byte of data. A flag is just a specific bit that signifies something. For example, if the first bit of the byte is a '1' it might mean that you've beaten a certain area of the game.


No comments:

Post a Comment