2016-02-27 51 views
-2

你好我想讀取位於我的程序的子文件夾中的文件。我成功地讀取了我的程序主文件夾中的文件。但是我無法讀取子文件夾中的文件。 我試圖添加一個名爲subdir =「/ * /」的新字符串,並且這樣做是爲了獲取子文件夾,但是程序拒絕使用我的字符串subdir獲取新路徑。是否有人對如何做到這一點有線索?我想出了一些想法。如何在C++中用fstream打開子文件夾

char resulta[ MAX_PATH]; 
    DWORD flicksa = GetModuleFileName(NULL,resulta,MAX_PATH); 
    _chdir(std::string(resulta,GetCurrentDirectory(flicksa,resulta)).c_str()); 
std::string path = "/Users/Aymann/Desktop/arraysort/2CPP-REVISIONS/Supcount/"; 
std::string searchPattern = "/*.cpp"; 
std::string fullSearchPath = path + searchPattern; 
WIN32_FIND_DATA FindData; 
HANDLE hFind; 

hFind = FindFirstFile(fullSearchPath.c_str(), &FindData); 

if(hFind == INVALID_HANDLE_VALUE) 
{ 
    cout << "Error searching directory\n"; 
    return -1; 
} 

do 
{ 
    char c; 
    string filePath = path + FindData.cFileName; 
    ifstream in(filePath.c_str()); 
    if(in.is_open()) 
    {in.get(c); 
     while (in) { 
     while (in && c != '\n') { 

      in.get(c); 
     } 
     _rowcount = _rowcount + 1; 
     in.get(c); 

     } // do stuff with the file here 
    } 
    else 
    { 
     cout << "Problem opening file " << FindData.cFileName << "\n"; 
    } 
} 
while(FindNextFile(hFind, &FindData) > 0); 

if(GetLastError() != ERROR_NO_MORE_FILES) 
{ 
    cout << "Something went wrong during searching\n"; 
} 

回答

0

您不能將通配符傳遞給fstream。

解決方案:下降文件夾,打開文件。

請參閱glob(),opendir(),readdir(),closedir()。

相關問題