|
-
Sep 18th, 2001, 05:19 AM
#1
Thread Starter
Member
file opening/saving
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
-
Sep 18th, 2001, 05:53 AM
#2
Frenzied Member
-
Sep 18th, 2001, 06:18 AM
#3
Thread Starter
Member
*says dûh to himself*
thanks
-
Sep 18th, 2001, 08:24 AM
#4
Thread Starter
Member
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*
^+^
-
Sep 18th, 2001, 11:04 AM
#5
Fanatic Member
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.
Code:
#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.
-
Sep 18th, 2001, 11:21 AM
#6
Thread Starter
Member
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:
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.
-
Sep 18th, 2001, 11:34 AM
#7
Fanatic Member
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.
Code:
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.
Code:
fout << map[w][h].Sort << " ";
there, hope that helps 
oh, and dont forget to close the file stream when ur done with it:
Code:
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.
-
Sep 18th, 2001, 11:44 AM
#8
Thread Starter
Member
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!
-
Sep 18th, 2001, 11:49 AM
#9
Actually, C file I/O is a best bet for this kind of thing. Compare your fstream code to:
Code:
//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.
-
Sep 18th, 2001, 11:50 AM
#10
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.
-
Sep 18th, 2001, 12:10 PM
#11
Fanatic Member
You;re right about the C-style code being better at this, C++ style code is good at writing "string" type of output.
-
Sep 18th, 2001, 12:11 PM
#12
Thread Starter
Member
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 ^_^
-
Sep 18th, 2001, 12:20 PM
#13
Create a Struct:
Code:
struct MAP
{
char MapName[64]; //64 char max map name
int map[MAX_XTILES][MAX_YTILES]; // map
};
Then, when you save:
Code:
//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.
-
Sep 18th, 2001, 12:22 PM
#14
Thread Starter
Member
ok, cool ^_^
Thank you both
-
Sep 18th, 2001, 02:22 PM
#15
Monday Morning Lunatic
Also, if you use CreateFile (part of the API) rather than the standard library functions you get the best performance, and the smallest code
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|