我正在尝试修改一些现有的 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;
}
目前,我收到编译器错误:编辑:编译器错误已修复,感谢 Blood!,但请参阅下文...
error: no matching function for call to 'std::basic_ofstream<char>::open<std::string&>
有什么想法吗?如果您需要更多代码/信息,请告诉我。另外,我应该补充一点,我在 Windows XP 上使用命令提示符运行这些程序。
谢谢。
编辑:
它现在可以编译(感谢 Blood),尽管当它运行时它只是试图打开目录,而不是目录中的文件。
Problem opening file directory_name.
ifstream 应该打开目录中的文件,而不是目录本身。
编辑 2:
我正在使用以下提示从命令行运行可执行文件:
.\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)
解决方案:
以下是代码的主要更改(感谢 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());
从命令行:
.\tag.exe C:\indir C:\outdir
非常感谢您的帮助。
最佳答案
确保将正确的 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);
关于C++ 循环遍历目录中的文件并写入不同的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11299687/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我脑子里浮现出一些关于一种新编程语言的想法,所以我想我会尝试实现它。一位friend建议我尝试使用Treetop(Rubygem)来创建一个解析器。Treetop的文档很少,我以前从未做过这种事情。我的解析器表现得好像有一个无限循环,但没有堆栈跟踪;事实证明很难追踪到。有人可以指出入门级解析/AST指南的方向吗?我真的需要一些列出规则、常见用法等的东西来使用像Treetop这样的工具。我的语法分析器在GitHub上,以防有人希望帮助我改进它。class{initialize=lambda(name){receiver.name=name}greet=lambda{IO.puts("He
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚