NO boost REQUIRED, which would be an overkill.
Use stat() (not cross platform though as mentioned by pavon), like this:
#include <sys/stat.h> #include <iostream> // true if file exists bool fileExists(const std::string& file) { struct stat buf; return (stat(file.c_str(), &buf) == 0); } int main() { if(!fileExists("test.txt")) { std::cerr << "test.txt doesn't exist, exiting...\n"; return -1; } return 0; }
Output:
C02QT2UBFVH6-lm:~ gsamaras$ ls test.txt ls: test.txt: No such file or directory C02QT2UBFVH6-lm:~ gsamaras$ g++ -Wall main.cpp C02QT2UBFVH6-lm:~ gsamaras$ ./a.out test.txt doesn't exist, exiting...
Another version (and that) can be found here.