It was just explained to you! Since you're using read(), you need to add the NULL terminator yourself.
Code:
int main()
{
TerrainHeader ter;
std::fstream inputfile("scape.ter", ios::in);
inputfile.read(ter.title, 7);
ter.title[7] = 0;
cout << ter.title;
return 0;
}
"TERRAGEN" cannot fit into a char array with 8 characters. You need 9 with the NULL. So:
Code:
typedef struct
{
char title[9];
char type[8];
int xpts;
int ypts;
} TerrainHeader;
int main()
{
TerrainHeader ter;
std::fstream inputfile("scape.ter", ios::in);
inputfile.read(ter.title, 8);
ter.title[8] = 0;
cout << ter.title;
return 0;
}
Or you could simplify things and just use get().
Code:
typedef struct
{
char title[9];
char type[8];
int xpts;
int ypts;
} TerrainHeader;
int main()
{
TerrainHeader ter;
std::fstream inputfile("scape.ter", ios::in);
inputfile.get(ter.title, 9);
cout << ter.title;
return 0;
}
[edit] Three replies! Wow. I was too busy adding colour to my post . . . [/edit]
[edit=2] Make that four replies. [/edit]