最近,我發現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