-
fstream question...
hi,
i am loading in a binary file into a 2d vector. i have it working but don't understand how it works! lol
here is the code....
Code:
int widthb[1];
int heightb[1];
int value1[1];
vector <vector<int> > mapdata;
int LoadMapData()
{
ifstream load;
load.open("test.map", ios::binary);
if (load.fail())
return false;
load.read((char*)widthb, 2);
load.read((char*)heightb,2);
mapxsize = *widthb;
mapysize = *heightb;
// this is to determine how big the map is.
mapdata.resize(mapxsize);
for (int i = 0; i < mapxsize; i++)
{
mapdata[i].resize(mapysize);
for (int j = 0; j < mapysize; j++)
{
load.read((char*)value1, 1);
mapdata[i][j] = *value1;
}
}
load.close();
when i declare value1 as "int value1[1]" if i don't add the little array bit at the end it will not load the correct value when i use the load.read(), but i don't understand why. could someone please explain it to me???? or point me to a site that will help me understand it better? Also if anyone can explain why i have had to use pointers in this code, i can use them a little, but do not understand them yet!:mad: lol
Thanks for any help u can give....
-
I think it's because you need to pass a character pointer to the read function, so you need to cast the address of value1 (if it's an integer). If you make value1 an array, you always pass the address of the array (don't need to add the & before it).
Code:
int value1;
load.read((char*)&value1, 1);
mapdata[i][j] = value1;
-