Thread: files

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    255

    files

    i was trying to just do a basic text fighting style game and ran across a problem.

    i have it set up to ask to user enter where you want to save DOS style but then when i goto save it or open it it gives me error:

    Code:
    in.open(save, ios::in);
    it wants text where save is like " " but i wanted what the user inputted right there anyway to fix that?
    hooch

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    did you declare the fstream object?

    fstream in;
    fstream out;


    in.open("file.txt", ios::in);
    out.file("file.txt", ios:ut);

    in.close();
    out.close();

    if save = a string, you will need to put
    save.c_str();

    because it take on a const parameter

  3. #3
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    It works fine for me

    -Make sure 'save' is of type char[] or string
    -If string use save.c_str()

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    yea its all declared cause i changed it to a random "blah.txt" and error went away

    so what save.c_str()? how do i use that?
    hooch

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by mrafcho001
    It works fine for me

    -Make sure 'save' is of type char[] or string
    -If string use save.c_str()

    If it's a string, then you need to use c_str() member function to convert it into a NULL terminated C-style character string.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    so

    in.open(save.c_str(),ios::in)?
    hooch

  7. #7
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Yup,
    Strings were introduced in C++ but a lot of functions and classes were kept from C and they are expecting a Zero Terminated String or in other words an array of chars with the last one being '\0'.

    When they created the string class they knew it had to be compatible with those functions so they made the member c_str() that acts as a zero terminated string.

    the way you use it:
    string.c_str()
    it will take care of itself from there

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    so if i used a classic char style string it would work fine??
    hooch

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ressources files
    By mikahell in forum Windows Programming
    Replies: 4
    Last Post: 06-19-2006, 06:50 AM
  2. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM