1. 程式人生 > >【C++】linux下頭文件io.h的巨坑

【C++】linux下頭文件io.h的巨坑

ride find rst gopro str hub details let ubuntu

摘要:采用 io.h 頭文件提供的函數讀取指定文件夾中多個文件(文件名沒有規律)

系統配置:ubuntu16.04, cmake編譯

讀取文件的代碼如下,

void getFilesAll(string path, vector<string>& files) {
    //文件句柄
    long hFile = 0;
    //文件信息
    struct _finddata_t fileinfo;
    string p;  
    if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1
) { do { if ((fileinfo.attrib & _A_SUBDIR)) { if (strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0) { //files.push_back(p.assign(path).append("\\").append(fileinfo.name)); getFilesAll(p.assign(path).append("
\\").append(fileinfo.name), files); } } else { files.push_back(p.assign(path).append("\\").append(fileinfo.name)); } } while (_findnext(hFile, &fileinfo) == 0); _findclose(hFile); } }

第一次編譯後報錯:

error: aggregate ‘*********’ has incomplete type and cannot be defined

解決方法: struct _finddata_t fileinfo; 將struct去除, _finddata_t fileinfo;

第二次編譯後報錯:

error: ‘_finddata_t’ was not declared in this scope

error: ‘_findfirst’ was not declared in this scope

error: ‘_A_SUBDIR’ was not declared in this scope

error: ‘_findnext’ was not declared in this scope

error: ‘_findclose’ was not declared in this scope

嘗試過很多方法,包括修改頭文件格式,修改CMakeLists.txt的include路徑,復制io.h文件到 /usr/include/ 路徑下,都失敗了。

最後在github的issue上看到有人提到,io.h 頭文件可能不兼容跨平臺操作。在windows下這個頭文件運行穩定,但是在linux下這個頭文件不能正常運行。

巨坑吶!!!是真的不兼容嗎,還是存在其他問題?求高人指點!

【C++】linux下頭文件io.h的巨坑