Skip to content

Instantly share code, notes, and snippets.

@scooby
Created February 8, 2009 05:35
Show Gist options
  • Save scooby/60217 to your computer and use it in GitHub Desktop.
Save scooby/60217 to your computer and use it in GitHub Desktop.
tests ios modes when file doesn't exist; which ones will create file
#include <fstream>
#include <iostream>
using namespace std;
void test(ios_base::openmode bits, char* name) {
fstream f;
f.open(name, bits);
cout << "Tried " << name;
if(f.is_open()) {
cout << " and was successful." << endl;
f.close();
} else {
cout << " and failed." << endl;
}
}
int main(void) {
ios_base::openmode m = ios_base::out;
test(m, "out");
test(m | ios_base::trunc, "out trunc");
test(m | ios_base::app, "out app");
test(m | ios_base::ate, "out ate");
m = ios_base::in;
test(m, "in");
test(m | ios_base::trunc, "in trunc");
test(m | ios_base::app, "in app");
test(m | ios_base::ate, "in ate");
m = ios_base::in | ios_base::out;
test(m, "inout");
test(m | ios_base::trunc, "inout trunc");
test(m | ios_base::app, "inout app");
test(m | ios_base::ate, "inout ate");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment