2012-07-02 89 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