最近,我发现Xcode可以用来编写普通的C++程序。XCode,标准C++和ifstream文件处理
我的问题是用下面的代码:
#include <iostream>
#include <fstream>
using namespace std;
int main (int argc, char *argv[]) {
char operation='0';
int operand=0;
//open file for reading
ifstream ifile;
ifile.open (argv[1]);
if(!ifile.is_open()){
cout<<"Invalid file\n";
exit(1);
}
while(ifile.good())
{
ifile>>operation;
if(operation=='I') {
ifile>>operand;
cout<<"Inserting :"<<operand<<endl;
}
else
if(operation=='D') { cout<<"Deleting "<<endl;
}
}
return 0;
}
当输入如下,
I 4
I 8
I 10
I 9
*
当我编译和Xcode中运行,我得到以下
Loading program into debugger…
Program loaded.
run
[Switching to process 58116]
Inserting :0
Running…
Debugger stopped.
Program exited with status value:0.
我已经在项目首选项中设置了文件名作为参数。
然而,在终端,我得到
Inserting :4
Inserting :8
Inserting :10
Inserting :9
请帮助我理解为什么会这样。
什么问题?它看起来很好。你在期待什么,你没有看到。 – Stabledog 2010-10-19 23:55:03
@Stabledog:调试器输出显示“Inserting:0”,没有别的。 – dreamlax 2010-10-19 23:57:14
只是一个与问题无关的观察:而不是'ifile.good()'你应该只使用'ifile'(隐式转换为'bool')或'!ifile.fail()'。 Iostreams'good'与'fail'不是相反的......重新提出这个问题,也许如果你在最后停止了程序,你会看到所有的输出结果? – 2010-10-20 00:02:15