Thread: File i/o

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    176

    File i/o

    ok i just need to know how i can make the file save in the directort i want it to, and how to be able to pass a variable into the name of the file ill show you the program im making and you'll see

    Code:
    #include <fstream>
    #include <iostream>
    using namespace std;
    int main()
    {
    char PlayerFirstName[50];
    char PlayerLastName[50];
    char PlayerFullName[100];
    
    char CharacterName[50];
    
    char Class[50];
    
    char Race[50];
    
    int hp;
    
    int xp;
    
    int Mana;
    
    int stop;
    
    std::cout<<"Enter Player First Name: ";
    std::cin>>PlayerFirstName;
    std::cout<<"Enter Player Last Name: ";
    std::cin>>PlayerLastName;
    strcat ( PlayerFullName, PlayerFirstName );    
    strcat ( PlayerFullName, " " );      
    strcat ( PlayerFullName, PlayerLastName );
    
    std::cout<<"\nEnter Character Name: ";
    std::cin>>CharacterName;
    
    std::cout<<"\nEnter Class: ";
    std::cin>>Class;
    
    std::cout<<"\nEnter Race: ";
    std::cin>>Race;
    
    std::cout<<"\nHow many Hitpoints?: ";
    std::cin>>hp;
    
    std::cout<<"\nHow much Experience?: ";
    std::cin>>xp;
    
    std::cout<<"\nyour player is.... \n \n \n\n\n\n\n";
    std::cout<<"Player Name: "<<PlayerFullName<<"\nCharacter Name: "<<CharacterName<<"\nclass: "<<Class<<"\nRace: "<<Race<<"\nHitpoints: "<<hp<<"\nExperience: "<<xp;
    ofstream Playerfile ( "player.txt" );
    Playerfile<<"Player Name: "<<PlayerFullName<<"\nCharacter Name: "<<CharacterName<<"\nclass: "<<Class<<"\nRace: "<<Race<<"\nHitpoints: "<<hp<<"\nExperience: "<<xp;
    std::cin>>stop;
    }

    what i want is what is passed to variable PlayerFullName to be what is the file name (instead of just player.txt) because i want to be able to creat a lot of these without erasing any also i want to be able to easily find my files i saved any help would be appreciated

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    i suggest you learn to use c++ strings,
    if your going to write c++ code,
    may want to google search, but here you
    go with this, should get you started look
    thru it see if you understand ask any question you may
    have :

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    	string PlayerFullName, CharacterName, Class, Race;
    	int hp, xp, Mana, stop;
    
    	cout<<"Enter Player Full Name: ";
    	getline(cin, PlayerFullName, '\n');
    
    	cout<<"\nEnter Character Name: ";
    	getline(cin, CharacterName, '\n');
    
    	cout<<"\nEnter Class: ";
    	getline(cin, Class, '\n');
    
    	cout<<"\nEnter Race: ";
    	getline(cin, Race, '\n');
    
    	cout<<"\nHow many Hitpoints?: ";
    	cin>>hp;
    	cin.ignore();
    
    	cout<<"\nHow much Experience?: ";
    	cin>>xp;
    	cin.ignore();
    
    	cout<<"\nyour player is.... \n \n \n\n\n\n\n";
    	cout<<"Player Name: "<<PlayerFullName<<"\nCharacter Name: "<<CharacterName<<"\nclass: "<<Class<<"\nRace: "<<Race<<"\nHitpoints: "<<hp<<"\nExperience: "<<xp;
    
    	string filename = PlayerFullName + ".txt";
    	ofstream Playerfile ( filename.c_str() );
    	Playerfile<<"Player Name: "<<PlayerFullName<<"\nCharacter Name: "<<CharacterName<<"\nclass: "<<Class<<"\nRace: "<<Race<<"\nHitpoints: "<<hp<<"\nExperience: "<<xp;
    	cin.get();
    	return 0;
    }
    PS
    I suppose i should mention that i didnt actually test this code
    i wrote it out farely quickly in notepad but it looks good
    some im sure it will work.

  3. #3
    Banned
    Join Date
    Jun 2005
    Posts
    594
    since i may not be on right away to answer your question
    ill go ahead and post some stuff that i think you may come
    to ask.


    getline(cin, string, '\n');

    this gets a line from cin, it stores it in the string
    option and it stop getting information from cin,
    when the \n character is reached, also know
    when you press enter. in the above code '\n'
    is known as the delimiter, and you can use any character
    as a delemiter.


    cin.get();

    this wait for the user to enter a key before
    the program moves on.


    cin.ignore();

    this removes the \n character off the buffer
    so as not to disturb the other cin function
    which would be terminated by the \n character.
    you can also specify how many to ignore for and a delimiter,
    so if you could ignore upto 100 character or until a white
    space is encountered by using cin.ignore(100, ' ');


    string filename = PlayFullName + ".txt";

    string have the ability to be added together,
    you can have a new string and combinded multiple
    string into it, or you can do string+=anotherstring
    to simply add on to that string.


    ofstream playerfile( filename.c_str() );

    for fstream to use string they must be null terminated
    so .c_str() make a string null terminated.


    using namespace std; / std::

    When using namespace std; you dont need
    to put std:: in front of everything.
    Last edited by ILoveVectors; 07-21-2005 at 11:05 PM.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    What the heck is the red about? Do you want to get your car shot up?
    Woop?

  5. #5
    Banned
    Join Date
    Jun 2005
    Posts
    594
    i just wanted make it stick out a little more,
    i would of used another color, but i thought that one
    would read well,
    and incase it wasnt noticeable the bold red,
    where links to more information on those topics

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    ok i understand all that, now i just need to be able to find the files easyer, so how can i make it save the .txt file in a specific place?

  7. #7
    Banned
    Join Date
    Jun 2005
    Posts
    594
    that right there will save it in the directory
    the executable is run from.
    so if that where you want it leave it like that
    other wise you could do :

    filename = "c:\\windows\\system32\\" + PlayerFullName + ".txt";


    and taht will save it under player name in the system32 directory
    as far as i know this only works as long as the directory exsist
    if it doesnt then you will get some kind of error,
    to create a directory try :

    http://msdn.microsoft.com/library/de...edirectory.asp

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    ok thanks i thought it would be something like that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM