Thread: Doubt c_str() on example at Prata's book.

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    10

    Doubt c_str() on example at Prata's book.

    Hello


    I'm reading Prata's C++ Primer Plus 6th and I have a doubt about an example code for I/O.
    On page 1117 there is a code that, among other things, says:


    Code:
    string filename;
    cout << "Enter filename";
    cin >> filename;
    ofstream fout(filename.c_str());

    The books says c_str() is used to pass the name of the file, that is a string.




    On page 1121 in a different example


    Code:
    fin.open(argv[number]);
    Why don't they use
    Code:
    .c_str()
    in this case?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,730
    What is the type of argv[number]? Is it the same type as filename as in your first code snippet?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Well, we'd have to know more about the type of the variable argv which you did not mention although I could probably take a safe guess. The open member function, as well as the constructor version you mention above, on older versions of the standard only accepted a character string (C-style, null terminated, character array) argument containing the filename. When you had a std::string object containing the filename you could not use this directly as an argument because this member function/constructor was not overloaded to accept a std::string argument. You had to use the c_str member function to convert a std::string object into something that would be accepted as an argument. Newer versions of the standard may allow you to directly use the std::string object without the need to go through the intermediary step of conversion to a const char* via the c_str member function.
    "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

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    10
    Hi

    Then the reason is that argv is a pointer to an array of characters instead of a string. ?

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Yes.
    "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 2012
    Posts
    10
    Quote Originally Posted by hk_mp5kpdw View Post
    Yes.
    Thanks

    I didn't understand why it can accept an array directly but not a string. I guess the reason is the string is a new class

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,532
    Actually if you're compiling the program with the current C++ standard (C++11) you can use either a std::string or a C-string.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does c_str() do?
    By Eman in forum C++ Programming
    Replies: 16
    Last Post: 01-18-2011, 09:17 AM
  2. c_str()
    By darren78 in forum C++ Programming
    Replies: 2
    Last Post: 08-14-2010, 03:07 AM
  3. C Primer Plus - Stephen Prata
    By Maksimilijan15 in forum C Programming
    Replies: 2
    Last Post: 08-02-2010, 01:32 PM
  4. A question to those using Prata's book.
    By subway in forum C Programming
    Replies: 1
    Last Post: 06-22-2010, 10:34 PM
  5. *this and .c_str()
    By stalker in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2004, 03:13 PM