tailieunhanh - C++ Programming for Games Module I phần 10

Tham khảo tài liệu 'c++ programming for games module i phần 10', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Introduction Many games do not expect the player to complete the game in one sitting and we can further assume that most gamers do not wish to start a game from the beginning each time they play. Therefore it is necessary that a game be able to be saved at certain points of progress and then resumed from those points at a later time. In order to satisfy this requirement we will need to be able to save load game information to from a place where it can persist after the program has terminated and after the computer has been turned off. The obvious place for such storage is the hard drive. Thus the primary theme of this chapter is saving files from our program to disk file output and loading files from disk into our program file input . Chapter Objectives Learn how to load and save text files to and from your program. Learn how to load and save binary files to and from your program. Streams Recall that cout and cin are instances of the class ostream and istream respectively extern ostream cout extern istream cin What are ostream and istream For starters the o in ostream stands for output and thus ostream means output stream. Likewise the i in istream stands for input and thus istream means input stream. A stream is a flow of data from a source to a destination. It is used analogously to a water stream. As water flows down a stream so data flows as well. In the context of cout the stream flows data from our program to the console window for display. In the context of cin the stream flows data from the keyboard into our program. We discuss cout and cin because file I O works similarly. Indeed we will use streams for file I O as well. In particular we instantiate objects of type ofstream to create an output file stream and objects of type ifstream to create an input file stream. An ofstream object flows data from our program to a file thereby writing saving data to the file. An ifstream object flows data from a file into our program thereby reading loading data from