First I'd start with a bit of design. A 'Book' class could be appropriate:
1 2 3 4 5 6 7 8 9
|
class Book
{
private:
map<char, int> myMap;
public:
void PopulateMap(char * filename);
int GetValueForChar(char key);
};
|
The 'myMap' member would be your associative array of characters to positions in your bookfile. This map could be populated in the 'PopulateMap' method. This method might set up a stream to read the bookfile, and populate the map with the chars/position pairs that it reads:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
char s;
int i=0;
// Open stream
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
// Get the next character and store it
// in the map with the position value
myfile.get(s);
myMap[s] = i++;
}
myfile.close();
}
|
Once your Book object is set up, encoding a file would simply involve reading chars from the messagefile, looking up the corresponding ints using the 'GetValueForChar' method, and outputting these integers (perhaps spaced or something) to your codedfile.
The decoding process would involve something similar. In the decoding program, the map in the Book class would map ints to chars, but it could still be populated using a similar approach in the 'PopulateMap' method.
There's likely a neater way to do this, but it's a start.
Don't forget to #include the relevant headers (i.e. iostream, fstream, map).
Hope that helps.