跨平台文件存在&读入C ++

时间:2010-06-26 22:16:10

标签: c++ file

如何编写两个简单的跨平台(Linux,Windows)函数来读取文本文件,并确定某个文件是否存在?

我不想使用像Boost.IO这样的大型库。对于某些软件来说,这是一个非常小的插件,我认为这不是必需的。

感谢。

2 个答案:

答案 0 :(得分:4)

标准库应该足够了。 access会告诉您文件是否存在,并且(如果存在)您可以使用正常的std::ifstream阅读。

答案 1 :(得分:0)

// 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(); 
}
相关问题