tags:

views:

83

answers:

2

Hi

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.

+4  A: 

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.

Jerry Coffin
A: 
// 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(); 
}