Thread: ofstream doesnt work with std::string?

  1. #1
    Stinking it up. StinkyRyan's Avatar
    Join Date
    Jun 2004
    Posts
    61

    ofstream doesnt work with std::string?

    I'm trying to take a user entered string and turn it into a file name.
    Code:
    string name;
    cout<<"Enter file name to save to: ";
    cin>>name;
    name = name + ".css";
    cout<<"Saving now..."<<endl;
    ofstream fo(name);
    compiler is saying:
    157 C:\Dev-Cpp\sbg\main.cpp
    no matching function for call to `std::basic_ofstream<char,
    89 C:\Dev-Cpp\include\c++\iosfwd
    candidates are: std::basic_ofstream<char,
    403 C:\Dev-Cpp\include\c++\fstream
    std::basic_ofstream<_CharT,

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Use c_str(), for example:
    Code:
    ofstream out(mystdstr.c_str());
    c_str() returns a const char*
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ofstream and FILE behaviour
    By MrLucky in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2007, 05:45 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. Replies: 9
    Last Post: 06-06-2002, 07:03 PM
  5. returning std::string
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2001, 08:31 PM