numbers and fstream

With my map editor, it uses wierd symbols that cant be saved to a text file, so instead I have to save the symbol numbers one by one. But I want a way to make it so it saves the numbers, putting a space between each one, and then recollects them. Heres what I have so far (Only a small part of my program... Figured nothing else is really needed):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
case 'S':
   {
    ofstream Write("Save.tx");
    for(int tX = 0; tX < 45; tX++)
    {
     for(int tY = 0; tY < 80; tY++)
     {
      Write << (int)TextMap[tY][tX] << " ";
     }
    }
    for(int cX = 0; cX < 45; cX++)
    {
     for(int cY = 0; cY < 80; cY++)
     {
      Write << ColorMap[cY][cX] << " ";
     }
    }
    Write.close();
    break;
   }
   case 'L':
   {
    ifstream Read("Save.tx");
    for(int tX = 0; tX < 45; tX++)
    {
     for(int tY = 0; tY < 80; tY++)
     {
      Read >> TextMap[tY][tX];
     }
    }
    for(int cX = 0; cX < 45; cX++)
    {
     for(int cY = 0; cY < 80; cY++)
     {
      Read >> ColorMap[cY][cX];
     }
    }
    cursPos.X = 0;
    cursPos.Y = 0;
    SetConsoleCursorPosition(hOut, cursPos);
    for(int X = 0; X < 80; X++)
    {
     for(int Y = 0; Y < 45; Y++)
     {
      cout << TextMap[X][Y];
     }
    }
    Read.close();    
    break;
   }
  }
Why don't you just open the file as binary?
Because I need the maps editable outside the map editor too.
A string of 3600 numbers isn't what I'd call human-readable.
Lol just how do I read a whole number within a space?? I need it to put 1 - 3 digit numbers in each spot of this... And I just want it that way because first off I need to write the map number at the end, a bunch of other stuff such as the weather variable, map temperature, etc. I guess I could add it to map editor, but I dont want to have to go to map editor and load every single map up one by one when Im ready to use in my game.
Since you want it to be editable outside your map editor, why don't you output chars in the file instead of ints?

EDIT: That's what you want this for, right? -> http://cplusplus.com/forum/beginner/27098/
Last edited on
Like I said, for some reason it wont output characters like ☺♣♠ to the text file... They just turn into | and other random symbols. But no the getch() thing was for selecting the hotkey. The hotkey is the current symbol you have to put on the map.
for some reason it wont output characters like ☺♣♠ to the text file... They just turn into | and other random symbols.
They probably *are* getting written correctly, but never forget that data has no meaning in itself. For example, "32" can mean "the space character", a shade of gray, a measure of air pressure around a microphone during a recording, a coordinate, and any number of different things. Characters below 32 and above 127 are almost guaranteed to get messed up when displayed with different text editors using their default settings, but that's not because the content is wrong, it's because the program is interpreting the data wrong.
Alright I guess I can save the characters, but thats the problem. I NEED to save the colors as numbers... I use the SetConsoleTextAttribute();, and it will sometimes go above 1 digit to do that. So how can I save numbers in array more than one number, and reload it again?
If you only want to store info for foreground color, well, there are only 16 colors available, you can just store them as letters as well, ('a', 'b', 'c', 'd', etc...). If you want to store info for both foreground and background color, just output two letters. I see here that you actually output 2 arrays, one for the character and one for the color. You can just output one array that has 3 letters in each slot, character, fore color, back color.
Last edited on
Not foreground color. Im using SetConsoleTextAttribute, so that has much more than just 10. Im not just using the colors 1 - 15, Im using all the ones above that which edits the background color of the text.
-.- So, how does that make my suggestion not applicable? You can have 16 foreground and 16 background colors which result in 16*16=256 color combinations. Though, if you store them as 2 letters, the letters of the alphabet (24>16) are more than enough to represent them.
Last edited on
Yeah but thats what Im trying to figure out... How to read everything before another space as one variable of the array, and not a new variable for each number. Such as this:
a b ab az z cd = iArray[6];
Besides that for now, I have another problem... For some reason whenever it saves it saves really odd... It saves it, but instead of sending the whole map to textfile, it somehow prints the first half TWICE... Heres the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <fstream>

using namespace std;
char TextMap[80][45];
int ColorMap[80][45];
int GridX = 0;
int GridY = 0;
int TextHotKey[10] = {0, 46, 35, 43, 62, 60, 61, 126, 5, 6};
int ColorHotKey[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int curTextHotKey = 1;
int curColorHotKey = 1;
char CMD;
ofstream Write;
ifstream Read;

int main()
{
 system("MODE CON: COLS=80");
 system("MODE CON: LINES=60");
 HANDLE hOut;
 COORD cursPos;
 hOut = GetStdHandle(STD_OUTPUT_HANDLE);
 bool runLoop = true;
 for(int X = 0; X < 80; X++)
 {
  for(int Y = 0; Y < 45; Y++)
  {
   TextMap[X][Y] = '.';
   ColorMap[X][Y] = 7;
  }
 }
 for(int X = 0; X < 80; X++)
 {
  for(int Y = 0; Y < 45; Y++)
  {
   cout << TextMap[X][Y];
  }
 }
 for(int X = 0; X < 79; X++)
 {
  cout << (char)205;
  if(X == 37)
  {
   cout << (char)203;
  }
 }
 cout << 
"ASCII Map Editor (80 x 45 characters) " << (char)186 << "Current Tile: ' '                       " << endl <<
"Keys:                                 " << (char)186 << "Tile Hotkeys:     Color Hotkeys:        " << endl <<
"'Q' = Quit to menu                    " << (char)186 << "1:                1:                    " << endl <<
"'S' = Save map                        " << (char)186 << "2:                2:                    " << endl <<
"'L' = Load map                        " << (char)186 << "3:                3:                    " << endl <<
"'c' = Change color                    " << (char)186 << "4:                4:                    " << endl <<
"'t' = Change tile                     " << (char)186 << "5:                5:                    " << endl <<
"'a' = Apply tile                      " << (char)186 << "6:                6:                    " << endl <<
"'H' = Change tile hotkeys             " << (char)186 << "7:                7:                    " << endl <<
"'L' = Load map                        " << (char)186 << "8:                8:                    " << endl <<
"'C' = Change color                    " << (char)186 << "9:                9:                    " << endl <<
"'L' = Load map                        " << (char)186 << "Cursor Position:                        " << endl;
 for(int X = 0; X < 79; X++)
 {
  cout << (char)205;
  if(X == 37)
  {
   cout << (char)202;
  }
 }
 cursPos.X = GridX;
 cursPos.Y = GridY;
 SetConsoleCursorPosition(hOut, cursPos);
 while(runLoop == true)
 {
  CMD = getch();
  switch(CMD)
  {
   case '8':
    if(GridY > 0)
    {
     GridY--;
    }
    break;
   case '2':
    if(GridY < 44)
    {
     GridY++;
    }
    break;
   case '4':
    if(GridX > 0)
    {
     GridX--;
    }
    break;
   case '6':
    if(GridX < 79)
    {
     GridX++;
    }
    break;
   case 't':
    CMD = getch();
    if(CMD >= '1' && CMD <= '9')
    {
     curTextHotKey = CMD-'0';
    }
    break;
   case 'c':
    CMD = getch();
    if(CMD >= '1' && CMD <= '9')
    {
     curColorHotKey = CMD-'0';
    }
    break;
   case 'a':
    TextMap[GridX][GridY] = TextHotKey[curTextHotKey];
    ColorMap[GridX][GridY] = ColorHotKey[curColorHotKey];
    SetConsoleTextAttribute(hOut,ColorHotKey[curColorHotKey]);
    cout << (char)TextHotKey[curTextHotKey];
    break;
   case 'S': 
   {
    Write.open("Save.bin", ios::out | ios::binary);
    for(int X = 0; X < 80; X++)
    {
     for(int Y = 0; Y < 45; Y++)
     {
      Write << (char)TextMap[Y][X];
     }
    }
    Write.close();
    break;
   }
   case 'L': <----------------LOADING HERE HERE
   {
    Read.open("Save.bin", ios::in | ios::binary);
    for(int X = 0; X < 80; X++)
    {
     for(int Y = 0; Y < 45; Y++)
     {
      Read >> TextMap[Y][X];
     }
    }
    Read.close();
    cursPos.X = 0;
    cursPos.Y = 0;
    SetConsoleCursorPosition(hOut, cursPos);
    for(int X = 0; X < 80; X++)
    {
     for(int Y = 0; Y < 45; Y++)
     {
      cout << (char)TextMap[Y][X];
     }
    }
    break;
   }
  }
  SetConsoleTextAttribute(hOut,7);
  cursPos.X = 56;
  cursPos.Y = 57;
  SetConsoleCursorPosition(hOut, cursPos);
  cout << GridX << "/" << GridY;
  cursPos.X = 54;
  cursPos.Y = 46;
  SetConsoleCursorPosition(hOut, cursPos);
  SetConsoleTextAttribute(hOut,ColorHotKey[curColorHotKey]);
  cout << (char)TextHotKey[curTextHotKey];
  for(int Hc = 0; Hc < 9; Hc++)
  {
   cursPos.X = 59;
   cursPos.Y = 48 + Hc;
   SetConsoleCursorPosition(hOut, cursPos);
   SetConsoleTextAttribute(hOut,ColorHotKey[1 + Hc]);
   cout << (char)TextHotKey[curTextHotKey];
  }
  for(int Ht = 0; Ht < 9; Ht++)
  {
   cursPos.X = 41;
   cursPos.Y = 48 + Ht;
   SetConsoleCursorPosition(hOut, cursPos);
   SetConsoleTextAttribute(hOut,ColorHotKey[curColorHotKey]);
   cout << (char)TextHotKey[1 + Ht];
  }
  SetConsoleTextAttribute(hOut,7);
  cursPos.X = GridX;
  cursPos.Y = GridY;
  SetConsoleCursorPosition(hOut, cursPos);
 }
}


And by the way, yes Im using binary instead... Ill make a seperate text file for the map number/all that.
Last edited on
If you open the file in binary mode you should use write() and read() instead of the << and >> operators.

http://cplusplus.com/reference/iostream/ostream/write/
http://cplusplus.com/reference/iostream/istream/read/
Give me a few seconds Im going to try and fix this...
Last edited on
Topic archived. No new replies allowed.