To open or re-open a file stream for output.
void open(const char* s, ios_base::openmode mode = ios_base::out);
The function open() opens a file stream for output. The default mode is ios::out, but may be any valid open mode (see below.) If failure occurs open() calls setstate(failbit) which may throw an exception.
There is no return.
| Opening Modes | stdio equivalent |
|---|---|
| Output only | |
| ios::out | "w" |
| ios::binary | ios::out | "wb" |
| ios::out | ios::trunc | "w" |
| ios::binary | ios::out | ios::trunc | "wb" |
| ios::out | ios::app | "a" |
| Input and Output | |
| ios::in | ios::out | "r+" |
| ios::binary | ios::in | ios::out | "r+b" |
| ios:: in | ios::out | ios::trunc | "w+" |
| ios::binary | ios::in | ios::out | ios::trunc | "w+b" |
| ios::binary | ios:: out | ios::app | "ab" |
// Before operation, the file ewl-test contained: // Chapter One #include <iostream> #include <fstream> #include <cstdlib> char outFile[] = "ewl-test"; int main() { using namespace std; ofstream out; out.open(outFile, ios::out | ios::app); if(!out.is_open()) {cout << "file not opened"; exit(1);} out << "\nThis is an annotated reference that " << "contains a description\n" << "of the Working ANSI C++ Standard " << "Library and other\nfacilities of " << "the Embedded Warrior Library."; out.close(); return 0; }
Result:
After the operation ewl-test contained
Chapter One
This is an annotated reference that contains a description of the Working ANSI C++ Standard Library and other facilities of the Embedded Warrior Library.