Luc*_*enK 1 c++ io file-io fstream ifstream
我编写了以下方法来检查我的程序是否与文件IO一起正常工作,但它绝对不起作用.我从inFile得到的只是"ELF",谁能告诉我为什么?我的对象与其他类型的istreams完美搭配.
void testFiles(int ct, char ** args)
{
if(ct<2){
cout<<"Invalid number of arguments. Must be two files, one for input, one for output."<<endl;
return;
}
ifstream inFile;
inFile.open(args[0]);
Tree<Word,int> x;
Word *key;
Word *val;
cout<<"Tree extracted from file: "<<endl;
while(inFile.good()&&inFile.is_open()){
key = new Word();
val = new Word();
inFile>>*key;
inFile>>*val;
if(!inFile.good()){
cout<<"Error: incomplete key-value pair:"<<key->getStr()<<endl;
break;
}
cout<<key->getStr()<<" "<<val->getStr()<<endl;
x[*key] = val->asInt();
delete key;
delete val;
}
inFile.close();
ofstream outFile;
outFile.open(args[1]);
cout<<"Tree as read from file:"<<endl<<x;
outFile<<x;
outFile.close();
}
Run Code Online (Sandbox Code Playgroud)
args [0] 不是你程序的第一个参数.它是可执行文件本身的名称.
发生的事情是你打开自己的可执行文件,而不是命令行中指定的文件,并且由于你的程序是linux二进制文件,你正在读取ELF二进制文件开头的魔术字符串,这是" ELF".
要修复错误,请将args [0]更改为args [1].
| 归档时间: |
|
| 查看次数: |
194 次 |
| 最近记录: |