ifstream 始终将"ELF"返回到对象
ifstream always returns "ELF" to object
我写了下面的方法来检查我的程序是否正确地处理文件IO,但它绝对不起作用。我从inFile得到的都是"ELF",有人能告诉我为什么吗?我的对象可以很好地与其他类型的流兼容。
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();
}
args[0]是不是程序的第一个参数。它是可执行文件本身的名称。
实际情况是,您打开的是自己的可执行文件,而不是命令行中指定的文件,由于您的程序是linux二进制文件,因此您正在读取ELF二进制文件开头的魔法字符串,即"ELF"。
修改args[0]为args[1]
您指定的是程序名,而不是第一个参数作为文件名。
检查你所做事情的有效性是个好主意。ie。检查args[1]
是否为空和/或file open…仅检查参数计数是不够的。
相关文章:
- 如何返回对象C++的数组
- 如何使用std::min和std::less返回对象
- 如何通过引用返回对象
- 如何安全地从 DLL 调用返回对象
- 如何在不销毁对象的情况下返回对象列表
- 什么更好?返回对象指针列表?或返回指向对象列表的指针?
- 使用返回对象的函数处理错误
- 何时返回指针与返回对象的一般经验法则?
- 如何使用运算符+重载函数正确返回对象?
- C++,class,一个返回对象的函数
- 运算符的返回对象 = C++ 中的重载
- 从队列返回对象的最快方法,但前提是队列有它
- 如何使用条件表达式返回对象指针?
- 使用构造函数从函数返回对象
- 隐式转换函数的返回对象时是否会影响性能?
- 返回对象时从'const DList<int>* const'到 'DList<int>*' [-fallowive] 的转换无效
- 按值和堆栈大小限制返回对象
- 我应该如何从函数返回对象?
- c++:何时传递指针与返回对象
- 返回对象会丢失属性