I am trying to change some existing C ++ code to work with my needs, but have never used C ++ before, I am having some difficulties.
My goal:
--> time and memory-intensive processes for preparation for each file in directory: open file; generate a tagged representation;
The reason I don’t just want to call a C ++ program for each file (for example, with a shell script) is because before the execution of the code below, the steps of preprocessing time and memory are performed (This will take about 45-60 seconds, and it takes only 2-5 seconds to execute the code).
I inserted the code section below. I want to read the arguments from the command line.
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; }
I am currently getting a compiler error: EDIT : compiler error fixed, thanks Blood !, but see below ...
error: no matching function for call to 'std::basic_ofstream<char>::open<std::string&>
Any thoughts? Please let me know if you need more code / information. In addition, I must add that I run them in Windows XP using the command line.
Thanks.
EDIT:
Now it compiles (thanks to Blood), but when it starts, it only tries to open the directory, not the files in the directory.
Problem opening file directory_name.
Ifstream should open files in the directory, not in the directory itself.
EDIT 2:
I run the executable from the command line with the following prompt:
.\tag.exe C:\indir C:\outdir
I also tried:
.\tag.exe C:\indir\* C:\outdir\
This lists all the files, but how can I capture them? Also, is there an easier way to change my code / input?
I also tried:
.\tag.exe C:\indir\ C:\outdir\
This gives: a directory for finding errors.
EDIT 3:
Using:
.\tag.exe "C:\indir\*" C:\outdir\
I get the output:
Problem opening file . Problem opening file .. Problem opening file 2967 Problem opening file 2966 Problem opening file 4707 etc. (100s)
Decision:
Here are the key changes to the code (thanks to Nate Kohl!):
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());
and from the command line:
.\tag.exe C:\indir C:\outdir
Help was greatly appreciated.