Thread: I/O help...

  1. #1
    ------------
    Join Date
    Jun 2005
    Posts
    79

    I/O help...

    Ok, i am trying to make my MUD write the players information to a file, which it does, but when i try to make it check if a file for a player exists before it tries to load it, i cant get it to work... here is the code for saving the player (which works perfectly...)
    Code:
    void saveChar(Stats &S)
    {
            ofstream fout;
            fout.open(S.name.c_str());
            fout<<S.name<<endl<<S.race<<endl;
            fout<<S.lvlexp<<endl<<S.maxlife<<endl;
            fout<<S.defense<<endl<<S.life<<endl;
            fout<<S.attack<<endl;
            fout.close();
            cout<<"Character saved."<<endl;
    }
    but while trying to load the character using this code:
    Code:
    void loadChar(Stats &S)
    {
            string choice;
                                                                                    
            cout<<"Name: "<<endl;
            cin>>choice;
                                                                                    
            if ( !choice.is_open() ) {
                    cout<<"Character doesnt exist";
                    nameChar(S);
            }
            else {
                    end();
            }
    }
    i get this error:
    Code:
    [montez@localhost eternity]$ c++ eternity.cpp -o eternity -Wall -O2
    eternity.cpp: In function `void loadChar(Stats&)':
    eternity.cpp:152: error: `is_open' undeclared (first use this function)
    eternity.cpp:152: error: (Each undeclared identifier is reported only once for each function it appears in.)
    and its driving me crazy... anyone know why its doing that? i have a feeling its something simple, but i dont know what it is
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What are you trying to do? The is_open() function is for file streams, not strings. You have to write a lot more code to be able to check if the character exists in the file or not.

  3. #3
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    What are you trying to do? The is_open() function is for file streams, not strings. You have to write a lot more code to be able to check if the character exists in the file or not.
    Exactly, you're trying a .is_open() function on the string, you have to create a file object with the string as the name and check if that file is_open().

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    As Daved pointed out, this line:
    Code:
    if ( !choice.is_open() )
    doesn't make sense: you can't open or close a string variable. You can open and close an ifstream object or an ofstream object, but 'choice' is neither of those types.

    To write to the file, you created an ofstream object, and similarly you have to create an ifstream object to read from a file. So, use the string you read in from the user and construct an ifstream object. The only twist is that you need a char array or a string literal for the ifstream and ofstream constructors. To get a char array/cstring when you have a string, you can use the string function c_str():

    mystring.c_str();

    Once you have an ifstream object, then you can use is_open() on it.
    Last edited by 7stud; 06-24-2005 at 12:30 PM.

  5. #5
    ------------
    Join Date
    Jun 2005
    Posts
    79
    ...ok im lost now... i didnt want it to read from the file to see if something was in it, i just wanted it to check and see if the file exists in the first place. the file is created under the persons name (name is goosie, so filename is goosie...) and i wanted it to check if the character exists by looking for the file
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    ...ok im lost now... i didnt want it to read from the file to see if something was in it, i just wanted it to check and see if the file exists in the first place.
    Well, is_open() does what it sounds like: it checks to see if a file "is open". If you don't want to check if a file is_open(), then don't use that function.

    In addition, you can't just take a C++ function you find and call it willy nilly with any object. An object has to be a member of the same class as a function to be able to call the function. A string object and the is_open() function are not in the same class, so a string cannot call is_open(), which is what you did.

    If you go here:

    http://cs.scsu.edu/C-C++%20Reference.htm

    and click on:

    C++ I/O

    you will see a list of all <fstream> functions, and as you can see there is no exists() function, so you are out of luck.

    You can check to see if a file exists indirectly by trying to open the file, i.e. creating an ifstream object with the filename. Then, if you use the ifstream object to call is_open(), and it returns false, then no file by that name exists in that directory.
    Last edited by 7stud; 06-24-2005 at 12:31 PM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    One technique would be to attempt to open the file first with a file stream, and then call is_open() on the file stream. So you need code similar to your saveChar function that creates an fstream (ifstream would be better than ofstream) and attempts to open it. Then just see if the open succeeds. There are other ways to see if a file exists, but this would be suitable for your needs.

  8. #8
    ------------
    Join Date
    Jun 2005
    Posts
    79
    ...wait, i just reolized that i did that... lol, i had it as something else and it just kept says "Character doesnt exist" and going to the part were it creates a character so i had changed something and i guess thats what it was...
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  9. #9
    ------------
    Join Date
    Jun 2005
    Posts
    79
    Ok, obviously i need to read up more on I/O so i'll just leave saving characters and loading them out of the MUD for now...
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Boost.Filesystem has an exists().
    http://www.boost.org/
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. asynchronized I/O == multiplexing I/O?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 07-24-2006, 10:06 AM
  2. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 07:42 AM
  3. Nonblocking I/O
    By fnoyan in forum Linux Programming
    Replies: 4
    Last Post: 11-29-2005, 04:37 PM
  4. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 05:14 PM
  5. WSAAsyncSelect I/O Mode :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 1
    Last Post: 05-12-2002, 03:23 PM