Using ofstream

This is a discussion on Using ofstream within the C++ Programming forums, part of the General Programming Boards category; Hi! I am trying to use ofstream. All of the examples I have seen online construct the stream like this: ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    8

    Using ofstream

    Hi! I am trying to use ofstream. All of the examples I have seen online construct the stream like this:
    insert
    Code:
    ofstream file_writer("my_file_name.txt").
    What I would like to do is to create a function that takes a string as an input, something like this:
    insert
    Code:
    int write_stuff_to_a_file(string file_name)
    {
        ofstream file_writer(file_name);
            ....
    }
    However, I get an error because file_name is not the correct type. I did some searching without much luck. I would appreciate some feedback about how to fix this problem. Thank you!

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You can get a c-style string representation of a C++ string object with c_str().
    Code:
    ofstream file_writer(file_name.c_str());
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,696
    The constructor for the C++ streams take a C-string, unless you are using a C++11 compiler. So you will need to use the string.c_str() member function to open your stream. Also remember if you declare your ofstream inside an if statement it will only be in the scope of the if statement. You may consider creating an instance before your if statement and opening it in the if.
    Code:
    ofstream file_writer;
    if(someValue)
       file_writer.open(file_name.c_str());
    Jim

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    8
    Thank you both! Why does c++ use c style strings. For backward compatibility?

  5. #5
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by aaleclaire View Post
    Thank you both! Why does c++ use c style strings. For backward compatibility?
    AFAIK (From the C++ Annotations book)
    When the first C++ standard was being made, the iostream and string libraries were made parallelly, each group without knowledge of the other's work, so ofstream and some others take a C style string as the argument.
    Though, from C++11 this is no longer the case, as you can use std::string `s for that purpose, as Jim said.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strings & ofstream
    By IM back! in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2008, 04:02 PM
  2. ofstream
    By Ducky in forum C++ Programming
    Replies: 54
    Last Post: 12-29-2007, 12:47 PM
  3. Using ofstream
    By alvifarooq in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2004, 09:06 PM
  4. If/Ofstream
    By SirCrono6 in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2003, 03:58 AM
  5. ofstream problem
    By XSquared in forum C++ Programming
    Replies: 4
    Last Post: 03-26-2003, 12:06 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21