setiosflag under the flow of the program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tarcisio
    New Member
    • May 2015
    • 2

    setiosflag under the flow of the program

    I want to change the mode of access to
    code
    ofstream transfer;
    \code

    so that I can alternately overwrite or append to it. What kind (type) of variable should I use to hold "ios::app" or "ios::out" ?

    This is the point at the program:


    code
    string label("2");
    .....
    some code that may chage label to "1";
    ..........
    if (label == "1") {transfer << setiosflags(met hod);};
    /code

    and I should have method to be either of "ios::app", "ios::out" .

    I have tried
    code
    string method;
    /code
    and then

    code
    setiosflags(met hod.c_str())
    /code

    withouth success. I know that ios::app is equivalent to 8 or is it equivalent to "8"?

    Thanks for help
    Tarcisio
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    setiosflags is a manipulator. You use it with the ofstream insertion operator:

    os << setiosflags(std ::ofstream::out | std::ofstream:: app);

    Mnaipulators are functions that take an ostream first argument, your own second argument and return and osteam&. This makes them useable with the ostream operator<<.

    MyOfstream.open ("Myfile.txt ", std::ofstream:: out | std::ofstream:: app);

    The openmode is a bitfield si it's neither 8 nor "8". It's std::ofstream:: app.

    Since ofstream derives from ios_base, you can also use ios::out | ios::app.

    Comment

    Working...