dealing with files.....

This is a discussion on dealing with files..... within the C Programming forums, part of the General Programming Boards category; ok, stripped down, this is what i use to save a player's game in my RPG: Code: saveinfo = fopen(savename, ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    106

    dealing with files.....

    ok, stripped down, this is what i use to save a player's game in my RPG:

    Code:
    saveinfo = fopen(savename, "w+");
    if (saveinfo == NULL) {
          cout << "ERROR\n";         	//if it can't save game produce 
                                                    //an error
    }
    else {			//write all records to whatever they 
                                                    //named the file
          for (a=0;a<276;a++) { fprintf(saveinfo, "%i\n", record[a]); }
          fclose(saveinfo);
    }
    if (quicksave==0 && saveinfo!=NULL) {
          cout << "\nGame saved successfully.\n";
          system("Pause");
    }
    savename is the variable for the name of the game that the player enters to save the game as. record[a] is all the different variables that are saved in the file.
    now, my question is this: i want to save all the games in a subdirectory of where the game is saved, say in a subdir called "saves". how would i do this if i dont know the directory that the game itself will be in? because it wont always be in c:/game/...i hope that makes sense. if anyone could give me some pointers i would appreciate it. thanks!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Here's a couple of ways

    1. Write an install program which prompts the user to enter the name of the dir where to install the game. This install program also generates a game.cfg containing the path to the saves (which you then read from within the game proper)

    2. use argv[0]
    Code:
    int main ( int argc, char *argv[] ) {
        printf("%s\n", argv[0] );
        return 0;
    }
    Some C runtime libs provide you with the full pathname of the executable.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    106
    thanks for the tips, ill look into em. never thought about the install thing...thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  4. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM
  5. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21