2012-07-02 116 views
4

我試圖修改一些現有的C++代碼來處理我的需求,但從未使用C++之前,我遇到了一些困難。C++循環遍歷目錄中的文件並寫入不同的目錄

我的目標是:

--> time and memory-intensive processes for preparation 

for each file in directory: 
    open file; 
    generate a tagged representation; //the current code just does this 
    write file; //different directory but same filename 

的原因,我不希望只是調用C++程序的每個文件(例如,一個shell腳本)是運行之前,下面的代碼,時間並執行內存密集型預處理步驟。 (這些需要大約45-60秒,而代碼只需要大約2-5秒。)

我粘貼了下面的代碼部分。我想從命令行讀取參數。

int main(int argc, char** argv) { 
    /* 
    pre-processing stuff 
    */ 

    /* for each file */ 
    HANDLE hFind = INVALID_HANDLE_VALUE; 
    string path = argv[1]; 
    string outpath = argv[2]; 
    WIN32_FIND_DATA ffd; 

    //EDIT 2: 
    cout << "Path: " << path << '\n'; 
    cout << "Outpath: " << outpath << '\n'; 

    hFind = FindFirstFile(path.c_str(), &ffd); 
    if (hFind == INVALID_HANDLE_VALUE) { 
    cout << "error searching directory\n"; 
    return false; 
    } 

    do { 
    //istream *is(&std::cin); 
    string filePath = path + ffd.cFileName; 
    ifstream in(filePath.c_str()); 
    if (in) { 
     /* for each line */ 
     string line; 
     int n = 1; 
     string str; 
     string fullOutpath = outpath + ffd.cFileName; 
     ofstream File; 
     File.open(fullOutpath); 
     while (getline(in, line)) { 
     if (line.size() > 1024) { 
      cerr << "warning: the sentence seems to be too long at line " << n; 
      cerr << " (please note that the input should be one-sentence-per-line)." << endl; 
     } 

     string postagged = bidir_postag(line, vme, vme_chunking, dont_tokenize); 

     /* output to file */ 
     File << postagged << endl; 
     //cout << postagged << endl; 

     /* increment counter */ 
     n++; 
     } 
     File.close(); 
    } else { 
     cout << "Problem opening file " << ffd.cFileName << "\n"; 
    } 
    } while (FindNextFile(hFind, &ffd) != 0); 

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

目前,我得到一個編譯器錯誤:編輯:編譯器錯誤固定的,由於血!見下文......

error: no matching function for call to 'std::basic_ofstream<char>::open<std::string&> 

有什麼想法?請讓我知道你是否需要更多的代碼/信息。另外,我應該補充一點,我使用命令提示符在Windows XP上運行這些程序。

謝謝。

編輯:

現在編譯(感謝血),不過當它運行它只是試圖打開該目錄,不在目錄中的文件。

Problem opening file directory_name. 

ifstream應該打開目錄中的文件,而不是目錄本身。

編輯2:

我運行可執行fromt他下面的提示命令行:

.\tag.exe C:\indir C:\outdir 

我也有嘗試:

.\tag.exe C:\indir\* C:\outdir\ 

此枚舉所有文件,但我如何捕獲它們?另外,是否有更簡單的方法來修改我的代碼/輸入?

我也曾嘗試:

.\tag.exe C:\indir\ C:\outdir\ 

這給:錯誤搜索目錄。

編輯3:

使用:

.\tag.exe "C:\indir\*" C:\outdir\ 

我得到的輸出:

Problem opening file . 

Problem opening file .. 

Problem opening file 2967 

Problem opening file 2966 

Problem opening file 4707 

etc. (100s) 

解決方案:

下面是該代碼的主要變化(感謝奈特科爾):

string path = argv[1]; 
path += "\\*"; 

hFind = FindFirstFile(path.c_str(),&ffd); 

    // in the 'do-while' loop 
    string filePath = argv[1]; 
    filePath += "\\"; 
    filePath += ffd.cFileName; 

    ifstream in(filePath.c_str()); 

    //regarding the outpath 
    fullOutpath = outpath + "\\"; 
    fullOutpath += ffd.cFileName; 
    File.open(fullOutpath.c_str()); 

,並通過命令行:

.\tag.exe C:\indir C:\outdir 

幫助是非常讚賞。

回答

4

確保您將正確的path格式傳遞給FindFirstFile

the documentation

To examine a directory that is not a root directory, use the path to that directory, without a trailing backslash. For example, an argument of "C:\Windows" returns information about the directory "C:\Windows", not about a directory or file in "C:\Windows". To examine the files and directories in "C:\Windows", use an lpFileName of "C:\Windows\*".


編輯:

我不是附近的Windows對話框現在(所以這可能無法編譯!),但我想,「循環遍歷目錄中的每個文件「看起來像這樣:

// argv[1] is the input path with no trailing characters, e.g. "c:\indir" 

// add a wildcard because FindFirstFile expects e.g. "c:\indir\*" 
TCHAR wildcard_path[MAX_PATH]; 
PathCombine(wildcard_path, argv[1], "*"); 

// iterate over each file 
WIN32_FIND_DATA ffd; 
HANDLE hFind = FindFirstFile(wildcard_path, &ffd); 
if (hFind == INVALID_HANDLE_VALUE) { } // error 

do { 
    // ignore directories 
    if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { 

     // create a full path for each file we find, e.g. "c:\indir\foo.txt" 
     TCHAR file_path[MAX_PATH]; 
     PathCombine(file_path, argv[1], ffd.cFileName); 

     // ...and do something with file_path. 
    } 
} while (FindNextFile(hFind, &ffd) != 0); 

FindClose(hFind); 
+0

這可能會def首先成爲問題的一部分,很好的抓住。 – user7116

+0

我從命令行運行編譯的可執行文件,並將文件指定爲C:\ indir \ * C:\ outdir \(我想這就是您的建議?)。在這種情況下,'path'成爲目錄中的第一個文件,outpath成爲indirectory中的第二個文件。另外,我得到上面的錯誤「Problem opening file 0064」(注意:0064是其中一個正確的文件名。 –

+0

@DavidC:它應該是'C:\ indir \ *'(注意尾部的'\ *') – user7116