passing ios::flag as a parameter ..

This is a discussion on passing ios::flag as a parameter .. within the C++ Programming forums, part of the General Programming Boards category; I have function saveData() which writes to a text file. I would like to tell it whether to append ios::app ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    passing ios::flag as a parameter ..

    I have function saveData() which writes to a text file. I would like to tell it whether to append ios::app or create new ios:ut ... How do i pass these flags as a parameter to saveData(...)?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,796
    One option is to have a std::ostream as the parameter. If you really want to open the file according to a filename and format flags then std::ios::fmtflags should be the type of the format flags parameter.
    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
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by laserlight View Post
    One option is to have a std::ostream as the parameter. If you really want to open the file according to a filename and format flags then std::ios::fmtflags should be the type of the format flags parameter.
    should it be this?

    Code:
    void saveData(std::ios::fmtflags flag) 
    {
          	ofstream outfile;
    	char filename[MAX_PATH] = "contract.txt";
    	outfile.open(filename, flag);
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,796
    Quote Originally Posted by csonx_p
    should it be this?
    It is probably correct but if you really need the filename variable, it would be better to write it as:
    Code:
    char filename[MAX_PATH] = "contract.txt";
    ofstream outfile(filename, flag);
    Actually, if you need filename, why not make it a std::string?
    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

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by laserlight View Post
    Actually, if you need filename, why not make it a std::string?
    because

    Code:
    void open ( const char * filename, ios_base::openmode mode = ios_base::out );

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,796
    Use the c_str() member function, e.g.,
    Code:
    std::string filename = "contract.txt";
    ofstream outfile(filename.c_str(), flag);
    EDIT:
    By the way, it looks like I got it wrong: the std::ios::fmtflags is for format flags (duh). On the other hand, the source you quoted gives std::ios_base:penmode, so perhaps you can try std::ios::openmode as the type.
    Last edited by laserlight; 01-05-2009 at 11:55 AM.
    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

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,354
    Quote Originally Posted by csonx_p View Post
    because

    Code:
    void open ( const char * filename, ios_base::openmode mode = ios_base::out );
    You can call c_str() on the string to get a char *...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parameter passing and pointers
    By pokiepo in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 12:13 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 07:26 PM

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