Saving a CHAR_INFO array to file...

Started by
4 comments, last by -JetSirus- 17 years, 9 months ago
EDIT: Saving issue semi-solved. See my later posts for more ineptitudness... I have been trying to come up with a good solution on how to save an array of CHAR_INFO types to a file so I can read them back in at a later time. Looking at the code for CHAR_INFO kind of throws me off.

typedef struct _CHAR_INFO {  
  union {
    WCHAR UnicodeChar;
    CHAR AsciiChar;
  } Char;
  WORD Attributes;
} CHAR_INFO,
  *PCHAR_INFO;
Looking at that it seems like it stores either a WCHAR or a CHAR and a WORD. So what I would need to do is find some way to write all that to a file in a manner that would be easy to read it all back in. Would it be best to just dump each item into the file in order? For example:
saveFile << infoChar[currentLoop].AsciiChar
<< infoChar[currentLoop].Attributes << "\n";  // newline is stop point for the current index
And then read them back in a similer fasion? I don't need to write the 'WCHAR' for my program since I will be dealing only with ascii. Any ideas? The reason why I am doing this is because I have made a little program to let me draw ascii art. I want to be able to save the drawing so I can then load it back into the game I am working on. [Edited by - -JetSirus- on July 27, 2006 8:50:22 PM]
------------------This is so stupid!
Advertisement
That's basically how I'd dump them, yeah. Although, in order to support Unicode more easily in the future, dump the attributes first, because the attributes should enable you to determine whether an ascii or unicode char is in use (and then dump the appropriate member of the union). When you read in, read the attributes, and then depending on their value, read the appropriate member.
You could probably get away with that if you're absolutely certain that nothing is using the UnicodeChar. Just to be safe though, you should save the UnicodeChar instead of the ascii one since the unicode one is the larger type. If you do that, the ascii should still work fine, since they really access the same memory.

This page explains unions in more detail if you're stuck.
Thanks for the replies!

Sadly, I think I may be in over my head here. Check this out:
/*   FILES_H   */#include <Windows.h>#include <string>bool SaveToFile(CHAR_INFO buffer[80*50], std::string fileName);/*   END FILES_H   *//*   FILES_CPP   */#include "files.h"#include <fstream>#include <string>#include <Windows.h>using namespace std;bool SaveToFile(CHAR_INFO buffer[80*50], string fileName){    ofstream fOut(fileName.c_str());    for(int i = 0; i < 80*50; i++)    {        fOut << buffer.Attributes << buffer.Char << "\n";    }        return true;}/*   END FILES_CPP   */

There are no logic statments in there yet to check for errors opening the file and stuff yet. Also I couldn't get past the delcaration of SaveToFile without '#include'ing headers in there. I don't know if that's bad or not. As it stands though I don't think I am handling this correctly at all.
error:  no match for 'operator<<' *snip*
That error seems to be talking about the CHAR_INFO struct. It seems plain as day that it doesn't support the '<<' operator. Im fairly new to working with filestreams and whatnot. Anyone around that knows more than me? I read around on MSDN. It explains the 'whats' but not the 'whys' and 'hows' on working with filestreams. At least I couldn't find much at all in the way of code examples.
------------------This is so stupid!
UPDATE: After tinkering around I got it to work.
#include "files.h"#include <fstream>#include <string>#include <Windows.h>using namespace std;bool SaveToFile(CHAR_INFO buffer[80*50], string fileName){    ofstream fOut(fileName.c_str());    DWORD singleAtt;    char singleChar;    for(int i = 0; i < 80*50; i++)    {        singleAtt = buffer.Attributes;        singleChar = buffer.Char.AsciiChar;        fOut << singleAtt << " " << singleChar << "\n";    }        return true;}

It sure isn't preaty, and it could go FUBAR at the drop of a hat. Thankfully I plan to go back and add some safty-net logic statments.
------------------This is so stupid!
sigh...

I must be a sucky programmer or something. When I save my array to a file I get some decent output. For example:

16
16
16 ±
16 ±
16 ±
16 Û
16 ±
16

Each line represent a single index of the array. The 16 being some number that goes into a WORD type. Followed by a space, then the Ascii character that goes into a (get ready for it) CHAR type. Now my issues start. I am not sure at all how to read that data back in. My code so far looks like this:

bool LoadFile(CHAR_INFO *buffer[], string fileName){    ifstream fIn(fileName.c_str());    WORD singleAtt;    char singleChar;    char trash;    for(int i = 0; i < 80*50; i++)    {        fIn >> singleAtt;        fIn >> singleChar;        *buffer.Char.AsciiChar = singleChar;        *buffer.Attributes = singleAtt;    }    return true;}

That has some BLARING issues. For one as far as my experiments have told me with the >> operator, when using it on a stream, it skips spaces. But since alot of if the data I am storing IS spaces I have problems. Even if that wasn't a problem, *buffer.Char.AsciiChar = singleChar is a problem. I don't even want to discuss the nastiness of the line right after it.. Basically I have no idea what the result of passing that WORD data back into .Attributes is going to be like. Does anyone with a kind heart have some spare time to explain to me how I might get this to work?
------------------This is so stupid!

This topic is closed to new replies.

Advertisement