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::out ... How do i pass these flags as a parameter to saveData(...)?
Printable View
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::out ... 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.
It is probably correct but if you really need the filename variable, it would be better to write it as:Quote:
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);
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::openmode, so perhaps you can try std::ios::openmode as the type.