2010-06-26 34 views
1

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

我不想使用像Boost.IO这样的大型图书馆。对于某些软件来说,它是一个非常小的插件,我不认为它是坏人。

谢谢。

回答

4

标准库应该足够了。 access会告诉你一个文件是否存在,如果它存在,你可以用正常的std::ifstream来读取。

+0

“access”的全名和类型是什么?它是一个功能还是一个班级?在Google上使用这个一般词汇进行搜索很困难。 – Notinlist 2012-08-23 09:07:14

+1

@Notinlist:我添加了一个可能有用的链接。 – 2012-08-23 13:24:12

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(); 
}