saving data (as binary?) and importing it into C++ as an integer?

This is a discussion on saving data (as binary?) and importing it into C++ as an integer? within the C++ Programming forums, part of the General Programming Boards category; I'm making a 2D tile RPG clone of dragon warrior (NES). I have a text file that looks like this: ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    8

    saving data (as binary?) and importing it into C++ as an integer?

    I'm making a 2D tile RPG clone of dragon warrior (NES).

    I have a text file that looks like this:

    1111111
    1000001
    1000001
    1111111

    Each digit represents a specific tile in a specific coordinate on the map.

    Anyway, I found out that when the file is saved as a text file, the data is read in (using fread()) in ASCII.

    I dont want this as then I am limited to storing 10 tiles as digits and the rest being other symbols. I would like to store all tiles as integers.

    I would then assume that I would need to change the external file to a binary format. How can I save a text file as binary? or just save directly to a binary file?

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    To open a file stream for reading in binary mode, either use ifstream as binary, or fstream with ios_base::in and ios_base::binary bitwise OR'd together:

    ifstream in ( filename, ios_base::binary );

    or

    fstream in ( filename, ios_base::in | ios_base::binary );

    It is also possible to open a file stream for both reading and writing by OR'ing together ios_base::in and ios_base::out.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    8
    Even with:

    ifstream in ( filename, ios_base::binary );

    The data is still coming into my program as ASCII.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Using binary mode to read in the data presumes that the data was written to the file as binary integers in the first place. If your text file truly just consists of zero and ones in character format I suggest you read in the data character by character and just convert to integers as you go along.

    How about showing us the code that writes and reads in the file along with the definition for the data structure you are storing the data in.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    8
    Here is what my code looks like:

    Code:
    struct MAP {
        BITMAP *map[20][15];
        unsigned int tileID[20][15];
    };
    
    ifstream mapInput ("worldmap.txt", ios_base::binary );
        
    for(int y=0; y<worldY; y++) {
        for(int x=0; x<worldX; x++) {
            tileID[x][y] = mapInput.get();
        }
    }
            
    mapInput.close();
    I'd also like to point out that my maps are not all 1's and 0's. They really could be any number.

    It would probably be best to separate the data with spaces or commas. I guess I'd actually prefer if my maps looked like this:

    001 001 001 001 001 001 001
    001 000 000 000 000 000 001
    001 000 000 255 000 000 001
    001 000 000 000 000 000 001
    001 001 001 001 001 001 001
    Last edited by BigToque; 03-19-2005 at 04:11 PM.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21