C++ file names

This is a discussion on C++ file names within the C++ Programming forums, part of the General Programming Boards category; Hi. I am making a text based rpg game (in C++) and am trying to get the game to save ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    9

    C++ file names

    Hi.

    I am making a text based rpg game (in C++) and am trying to get the game to save and load different characters.

    Character is a class I defined. Basically, I have all of the class's data loaded into arrays, and then I output those arrays to the file.

    Before, I had the program open the game file and save to it.
    Now I want it to create a file based on the Character's name, and save to the new file.

    The problem is, I don't know how to change file names or create files based upon a string variable.


    Code:
    void save(Character & You)
    {
    	string Ystr[2], n1, n2;
    	string name;
    	char n[25];
                    int Yint[23], ar1[4], sw1[6], ite[4];
    	int z;
    	name = You.name;
    	name = "c:\\darkland\\" + name + ".out";
    	
    	for(int i = 0; i<25; i++)
    	{
    		if(name.at(i) != ' ')
    			n[i] = name.at(i);
    	}
    	
                          //tofile.open("c:\\darkland\\game.out"); 
                          //the above is my previous file
    
    	tofile.open(n);     
    	system("CLS");							  
    	cout << "....Saving...." << endl;
    
    	You.name + ".out");
    
    	You.arrayStats(Ystr, Yint);			//fills class arrays
    	You.Armor1.getvars(n1, ar1);		//fills armor arrays
    	You.Sword1.getvars(n2, sw1);		//fills sword arrays
    	You.Items.getvars(ite);				//fills item arrays
    
    	//Global Vars Saved
    	tofile << checkpoint << endl;
    	tofile << north << endl;
    	tofile << east << endl;
    
    	tofile << Ystr[0] << endl;		//saves character strings
    	tofile << Ystr[1] << endl;
    	for(z=0; z < 23; z++)
    	{
                           tofile << Yint[z] << endl;	    //saves character stats
    	}		
    	
    	tofile << n1;
    	for(z=0; z < 4; z++)
                            tofile << ar1[z];	//enters armor into file
    
    	tofile << n2;
    	for(z=0; z < 6; z++)
                            tofile << sw1[z];	// enters sword into file
    
    	for(z=0; z < 4; z++)
                           tofile << ite[z];	       //enters items into file
    
    	tofile.close();
    	pause();
    }
    Last edited by enderbean719; 05-30-2006 at 07:03 PM.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    You could just write the instance of a Character class into a binary file, and read from it to "load" the data. This has the benefit of making the file illegible to human readers.

  3. #3
    Registered User
    Join Date
    May 2006
    Location
    irc.abstracttech.com
    Posts
    8
    what do you mean you dont' know how to change file names or create files based upon a string variable?

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    tofile.open(name.c_str());
    Changes the C++ string to a C string.

  5. #5
    Registered User
    Join Date
    May 2006
    Location
    irc.abstracttech.com
    Posts
    8
    just replace

    string name;
    char n[25];

    with

    char name[25];

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    No. Then he would not be able to do

    Code:
    name = You.name;
    And other things

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    9

    It worked!

    Quote Originally Posted by Tonto
    Code:
    tofile.open(name.c_str());
    Changes the C++ string to a C string.
    Thanks man! It worked!

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    9

    Loading

    Now I am trying to load the Character. I have it ask the user what name to load, but it still loads if the name is incorrect.

    How do I error check the name of the file?

    Code:
    string Ystr[2], n1, n2;	
    	string name;
    	int Yint[23], ar1[4], sw1[6], ite[4];
    	int z;
    	cout << "Type the name of the Character you want to load." << endl;
    	cin.ignore(100, '\n');
    	getline(cin, name);
    	name = "c:\\darkland\\" + name + ".out";
    
    	fromfile.open(name.c_str());	
    	system("CLS");							  
    	cout << "....Loading...." << endl;

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    After open(), you can use is_open() (I forgot the name but it goes along those lines) and good() to check if the file is indeed avaliable for reading.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Code:
    	int Yint[23], ar1[4], sw1[6], ite[4];
    	int z;
    No one will want to debug that for you. For free, that is.


    And system("cls") is bad. Read the FAQ.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    9

    Incorrect loading

    Thanks for the help jafet.
    The is_open() seems to work fine, but I couldn't figure out what to do with good().


    Here is the problem:

    When I type the correct file name at the first cin, it loads fine.

    But when I type the filename correctly after entering the error check loop, it seems to load incorrectly - as if pulling data from an empty file.

    Code:
    cout << "Type the name of the Character you want to load." << endl;
    cin >> name;
    name = "c:\\darkland\\" + name + ".out";
    fromfile.open(name.c_str());
    
    while (!fromfile.is_open())
    {
                    //i tried putting fromfile.close() here but it didn't seem to do anything
    
                    cout <<"File name is incorrect. Try again." << endl;
    	cin >> name;
    	
    	name = "c:\\darkland\\" + name + ".out";
    	fromfile.open(name.c_str());
    }

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    May-be you'll need fromfile.clear() to reset the error flags?

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    9

    Thanks.

    Thanks!

    The fromfile.clear() worked!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File being filled with NULLs
    By Tigers! in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2009, 06:28 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 08:51 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 08:01 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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