Click to See Complete Forum and Search --> : file opening/saving
Usako
Sep 18th, 2001, 05:19 AM
I saw a few methods but I dunno which one to use (and howto ^_^).
Anyway, I saw the following ones:
CreateFile
ofstream/ifstream
and some c method I think, dunno excactly what it was called.
Soooo, which one is the best one to use and please a short explanation how it works ^_^
Thankies
Vlatko
Sep 18th, 2001, 05:53 AM
Have a look at the FAQ.
Usako
Sep 18th, 2001, 06:18 AM
*says dûh to himself*
thanks :)
Usako
Sep 18th, 2001, 08:24 AM
I don't really like the method used in the faq...maybe I'm using it wrong or something ^_^.
I'll explain what I want to do:
i want to save (and load) a big map (1000x1000 now but it can get bigger). I have an array (map[MAP_XTILES][MAP_YTILES]) and it hold integer value's. Whats the best way to store it? I wasn't able to do it the faq way...*snif*
^+^
MoMad
Sep 18th, 2001, 11:04 AM
Hey Usaki, I got a sollution for you. For the sake of simplicity and all of the underlying power of fgets and fputs, use the iostream library.
#incude <fstream.h> // file i/o
// constants go here
int main(void)
{
ofstream fout;
ifstream fin;
int tempMap[width][height] = null; // null is defined to be 0 in C++
int temp;
// load map
for( int w = 0; w < width; w++ )
{
for( int h = 0; h < height; h++ )
{
fin >> temp;
tempMap[w][h] = temp;
}
}
// now you do something to ur map and u save it like....
// save it
for( int w = 0; w < newwidth; w++ )
{
for( int h = 0; h < newheight; h++ )
{
fout << tempMap[w][h];
}
fout << '\n'; // new line...
}
return 0;
}
That is all! I hope that has helped you. The fstream is my favorite!! it is so simple and easy yet fast and furious.
Usako
Sep 18th, 2001, 11:21 AM
yeah, that's the method I got the most succes with so far but it still doesn't work 100% ^_^.
Lemme show you the code I'm using now, based on your code:
//this just makes some random map :)
for (intX = 0; intX < MAP_XTILES; intX = intX + 1)
{
for (intY = 0; intY < MAP_YTILES; intY = intY + 1)
{
map[intX][intY].Sort = rand()%1600;
}
}
ofstream fout("map.map");
ifstream fin("map.map");
// load map
for( int w = 0; w < MAP_XTILES; w++ )
{
for( int h = 0; h < MAP_YTILES; h++ )
{
fin >> temp;
map[w][h].Sort = temp;
}
}
// now you do something to ur map and u save it like....
// save it
for( int w = 0; w < MAP_XTILES; w++ )
{
for( int h = 0; h < MAP_YTILES; h++ )
{
fout << map[w][h].Sort;
}
fout << '\n'; // new line...
}
but the file is just one big mess o numbers like:
14107109273102 etc etc.
and when I load it it just see's everything like 0's.
MoMad
Sep 18th, 2001, 11:34 AM
wait, a better idea is to not use the file name as a constant to open it with the constructor, there is an open method.
fin.open(pfileName, ios::nocreate|ios::in);
// the second parameter is, for "reading" and do not create a new file if it doesnt exist.
if( !fin.is_open() )
{
cerr << "the error";
//or just exit or just create one.
}
and for fout:
fout.open(pFileName);
// you do not need extra params since ur creating this file on the fly
tyry that u might get better results.
and also, use spaces to avoid making it one big chunk of a number.
fout << map[w][h].Sort << " ";
there, hope that helps :)
oh, and dont forget to close the file stream when ur done with it:
fin.close(); fout.close();
at the end of your code. dont worry the close method doesnt care wether the file is open or not. it has an if stament inside to check if its open and close it or just do nothing if its already closed.
Usako
Sep 18th, 2001, 11:44 AM
thanks alot, now it works ^_^. It's ok for now, later I have to find something to decrease the size (the map files are 4 meg :P)
Thanks!
Zaei
Sep 18th, 2001, 11:49 AM
Actually, C file I/O is a best bet for this kind of thing. Compare your fstream code to:
//Saving, whole map in one call
FILE* f = NULL;
f = fopen("map.map", "w+b");
fwrite(&map[0][0], 1, MAP_XTILES*MAP_YTILES*sizeof(map[0][0]), f);
fclose(f);
//Opening
f = fopen("map.map", "r+b");
fread(&map[0][0], 1, MAP_XTILES*MAP_YTILES*sizeof(map[0][0]), f);
fclose(f);
Z.
Zaei
Sep 18th, 2001, 11:50 AM
That will also reduce the file size, using 2 bytes per value, instead of one per number in the value (ie, 2 bytes for 3456, instead of 4).
Z.
MoMad
Sep 18th, 2001, 12:10 PM
You;re right about the C-style code being better at this, C++ style code is good at writing "string" type of output.
Usako
Sep 18th, 2001, 12:11 PM
yeah, that method works too ^_^...and it decreased the size with 200 kb.
But can you explain how I can load the values in diferent variables with that method?
Like if I first store the map name in the file and then the map data, how do I add the map name to the mapnamevar and the map data to map[x][y]?
Thanks ^_^
Zaei
Sep 18th, 2001, 12:20 PM
Create a Struct:
struct MAP
{
char MapName[64]; //64 char max map name
int map[MAX_XTILES][MAX_YTILES]; // map
};
Then, when you save:
//where someMap is of struct MAP
fwrite(&someMap, sizeof(MAP), 1, f);
and to open
fread(&someMap, sizeof(MAP), 1, f);
That will load both the map name, and data in one call.
Z.
Usako
Sep 18th, 2001, 12:22 PM
ok, cool ^_^
Thank you both :)
parksie
Sep 18th, 2001, 02:22 PM
Also, if you use CreateFile (part of the API) rather than the standard library functions you get the best performance, and the smallest code :)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.