2010-06-26 37 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(); 
}