Results 1 to 3 of 3

Thread: file i/o

  1. #1

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238

    file i/o

    I have a book on c++ (sams teach yourself), but it doesn't go into file input and output very well. What i need to do is this:
    1. Save a bunch of info about a level in a text file, so i can just load the info about each game level into a struct or maybe a class. The info would be like level parameters and such.
    2. I need to write to a file in order to save the game. This would be typical game save info.

    Preferably, I would like the file to be formatted like this:
    Level:1
    NumEnemies:10
    Background:C:\picture.bmp
    etc.....

    then i could just "pick" out the values
    i've done this in vb, but i can't find a good resource about it in c++. So if you could give me a hand or point me in the right direction i'd be real grateful.

    thanks,
    jmiller

  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    the fstream header contains two classes for file I/O: ifstream and ofstream for input and output respectively. you can also use the fstream class and specify what type of access you want to the file.
    Code:
    #include <fstream>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
         //input file stream
         ifstream in;
         string s;
         
         in.open("C:\\test.txt");
         while(!in.eof())
         {
              // read a single word
              in >> s; // or getline(in, s) for an entire line.          
              cout << s << endl;
              // if you were using an ofstream
              // you could write to it with the 
              //  stream insertion operator (<<)
         }
    
         in.close();
    
         return 0;
    }
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Are you using VC++7? If so I have a complete config file reader/writer...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width