Results 1 to 15 of 15

Thread: file opening/saving

  1. #1

    Thread Starter
    Member Usako's Avatar
    Join Date
    May 2001
    Posts
    60

    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

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Have a look at the FAQ.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3

    Thread Starter
    Member Usako's Avatar
    Join Date
    May 2001
    Posts
    60
    *says dûh to himself*

    thanks

  4. #4

    Thread Starter
    Member Usako's Avatar
    Join Date
    May 2001
    Posts
    60
    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*

    ^+^

  5. #5
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    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.
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  6. #6

    Thread Starter
    Member Usako's Avatar
    Join Date
    May 2001
    Posts
    60
    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.

  7. #7
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    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.
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  8. #8

    Thread Starter
    Member Usako's Avatar
    Join Date
    May 2001
    Posts
    60
    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!

  9. #9
    Zaei
    Guest
    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.

  10. #10
    Zaei
    Guest
    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.

  11. #11
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    You;re right about the C-style code being better at this, C++ style code is good at writing "string" type of output.
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  12. #12

    Thread Starter
    Member Usako's Avatar
    Join Date
    May 2001
    Posts
    60
    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 ^_^

  13. #13
    Zaei
    Guest
    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.

  14. #14

    Thread Starter
    Member Usako's Avatar
    Join Date
    May 2001
    Posts
    60
    ok, cool ^_^

    Thank you both

  15. #15
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
  •  



Click Here to Expand Forum to Full Width