Results 1 to 5 of 5

Thread: C++ ofstream::open won't accept string as a filename

  1. #1
    Join Date
    May 2009
    Location
    Planet Earth
    Beans
    220
    Distro
    Ubuntu 14.04 Trusty Tahr

    Angry [SOLVED] C++ ofstream::open won't accept string as a filename

    I'm including iostream, fstream, string, and cstdlib headers. In my program, if I have

    Code:
    ofstream os;
    string file = "infoFile.txt";
    
    os.open(file, ios::app);
    upon compilation I see the error

    Code:
    NoteOrganizer.cpp:28:31: error: no matching function for call to ‘std::basic_ofstream<char>::open(std::string&, const openmode&)’
    NoteOrganizer.cpp:28:31: note: candidate is:
    /usr/include/c++/4.6/fstream:702:7: note: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char, _Traits = std::char_traits<char>, std::ios_base::openmode = std::_Ios_Openmode]
    /usr/include/c++/4.6/fstream:702:7: note:   no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
    but if I instead do

    Code:
    os.open("infoFile.txt", ios::app);
    it compiles and runs just fine. I've used strings as file names countless times before, though, with no trouble at all. What is going wrong?
    (BTW: I'm using g++)

    Thanks!
    Last edited by chellrose; May 9th, 2013 at 06:00 PM. Reason: Trying to mark this as solved
    int main() {
    User chellrose;
    chellrose.insert_clever_signature();
    return 0;}

  2. #2
    Join Date
    Mar 2006
    Beans
    837

    Re: C++ ofstream::open won't accept string as a filename

    As the error message says to you there isn't an open() method which accepts std::string. The open() method has this signature void open( const char *filename,
    ios_base::openmode mode = ios_base::out );
    So you could do:
    Code:
    os.open(file.c_str(), ios::app);
    Or make gcc use the C++11 standard which has a suitable open() function which takes an std::string. I believe you have to use the "-std=c++11" switch when compiling.

    Relevant info on open()->http://en.cppreference.com/w/cpp/io/basic_ofstream/open

  3. #3
    Join Date
    May 2009
    Location
    Planet Earth
    Beans
    220
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: C++ ofstream::open won't accept string as a filename

    Right! I forgot about the c_str().

    Thanks!

    ***note to all: This thread is solved... Once I figure out how to get the "prefix" option back, I'll mark it solved for real
    Last edited by chellrose; May 2nd, 2013 at 02:08 AM.
    int main() {
    User chellrose;
    chellrose.insert_clever_signature();
    return 0;}

  4. #4
    Join Date
    May 2008
    Location
    UK
    Beans
    1,451
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: C++ ofstream::open won't accept string as a filename

    You can mark a thread as [SOLVED] by using the Thread Tools (near the top of the page).
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  5. #5
    Join Date
    May 2009
    Location
    Planet Earth
    Beans
    220
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: C++ ofstream::open won't accept string as a filename

    That's how I used to do it (in the old forum), but that option doesn't appear under Thread Tools now. Is there another trick that I'm missing?
    int main() {
    User chellrose;
    chellrose.insert_clever_signature();
    return 0;}

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •