Skip to content

Instantly share code, notes, and snippets.

@disconnect3d
Created September 21, 2018 10:44
Show Gist options
  • Save disconnect3d/e0bdf4da728d185d620587f2f2a2eb94 to your computer and use it in GitHub Desktop.
Save disconnect3d/e0bdf4da728d185d620587f2f2a2eb94 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>

#define LOG(x) std::cout << #x << " = " << x << std::endl

int main() {
    {
        std::cout << "ofstream(\"/tmp\")" << std::endl;
        std::ofstream f("/tmp");

        LOG(f.good());
        LOG(f.eof());
        LOG(f.fail());
        LOG(f.bad());
        LOG(f.rdstate());
    }
    std::cout << std::endl;
    {
        std::cout << "ifstream(\"/tmp/\")" << std::endl;
        std::ifstream f("/tmp");

        LOG(f.good());
        LOG(f.eof());
        LOG(f.fail());
        LOG(f.bad());
        LOG(f.rdstate());
    }
    std::cout << std::endl;
    {
        std::cout << "ifstream(\"/tmp/notexistingfile\")" << std::endl;
        std::ifstream f("/tmp/notexistingfile");

        LOG(f.good());
        LOG(f.eof());
        LOG(f.fail());
        LOG(f.bad());
        LOG(f.rdstate());
    }
}

Output:

ofstream("/tmp")
f.good() = 0
f.eof() = 0
f.fail() = 1
f.bad() = 0
f.rdstate() = 4

ifstream("/tmp/")
f.good() = 1
f.eof() = 0
f.fail() = 0
f.bad() = 0
f.rdstate() = 0

ifstream("/tmp/notexistingfile")
f.good() = 0
f.eof() = 0
f.fail() = 1
f.bad() = 0
f.rdstate() = 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment