Cross Platform File Exists & Read In C++
How can I write two simple cross platform (Linux, Windows) functions for reading text file, and determine if some file exists?
I don't want to use 开发者_运维技巧a big library like Boost.IO for that. It's a very small plugin for some software and I don't think it's neccsary.
Thanks.
The standard library should be sufficient. access
will tell you if a file exists, and (if it's there) you can read with a normal std::ifstream
.
// portable way to get file size; returns -1 on failure;
// if file size is huge, try system-dependent call instead
std::ifstream::pos_type filesize(const char* filename)
{
std::ifstream in(filename, std::ifstream::in | std::ifstream::binary);
in.seekg(0, std::ifstream::end)
return in.tellg();
}
精彩评论