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(...)?
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 ...
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(...)?
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.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
It is probably correct but if you really need the filename variable, it would be better to write it as:Originally Posted by csonx_p
Actually, if you need filename, why not make it a std::string?Code:char filename[MAX_PATH] = "contract.txt"; ofstream outfile(filename, flag);
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
Use the c_str() member function, e.g.,
EDIT:Code:std::string filename = "contract.txt"; ofstream outfile(filename.c_str(), flag);
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.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way