Jump to content
  • Advertisement
Sign in to follow this  
skulldrudgery

Saving and loading CHAR_INFO

This topic is 4887 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to take a text file, read all the characters into an array (in ASCII mode) and then convert it(one by one, for now. unless there is a better way)to CHAR_INFO and output the new array of CHAR_INFO to a file(in binary mode). Then I want to read the CHAR_INFO from the file(again in binary) to a different array. It used to crash because of some problem with array indices, but I fixed that. I doesn't crash, but I'm getting garbage when I write the new CHAR_INFO array to the screen. This is what I use to read the array and write the new one:
int main()
{
    CHAR_INFO screen[WIDTH * HEIGHT];
    
    ifstream load("blah.txt");
    
    for(int y = 0; y < HEIGHT; y++) {
        for(int x = 0; x < WIDTH; x++) {
            load >> screen[x + WIDTH * y].Char.AsciiChar;
            screen[x + WIDTH * y].Attributes = WHITE;
        }
    }
    
    ofstream fout( "map", ios::binary );
    
    for(int y = 0; y < HEIGHT; y++)
        for(int x = 0; x < WIDTH; x++)
            fout.write((char*)(&screen[x + WIDTH * y]), sizeof(screen[x + WIDTH * y]));

    return 0;
}






And this is what I use to read the new array:
void loadScene(std::string fileName)
{
    std::ifstream fin(fileName.c_str(), std::ios::binary);
    
    for ( int y = 0; y < HEIGHT; y++ ) {
        for ( int x = 0; x < WIDTH; x++ ) {
            fin.read((char*)(&screenBuffer[x + WIDTH * y]), sizeof(screenBuffer[x + WIDTH * y]));
        }
    }
}








And then I just use WriteConsoleOutput() to print it. Thanks for any help. WIDTH and HEIGHT are both const ints with values of 80 and 50, respectively. PS: Obviously, any disclaimers about n00b code apply. [Edited by - skulldrudgery on July 27, 2005 12:57:12 AM]

Share this post


Link to post
Share on other sites
Advertisement
I assume blah.txt is formatted correctly so that your first loop in main() fills screen with the correct values? Also, I recommend that rather than writing the entire structure you instead write each member individually.

Share this post


Link to post
Share on other sites
Ok, I'm a bit of a clown. I forgot that C++ streams ignore whitespace so my array was only filled with about 100 characters and the other 3900 was filled with garbage.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!