次の.txtファイルがあります。
test.txt
1,2,5,6
次のようにコマンドラインから作成した小さなC++プログラムに渡します。
./test test.txt
ソースは次のとおりです。
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char **argv)
{
int temp =0;
ifstream file;
file.open(argv[1]);
while(!file.eof())
{
temp=file.get();
file.ignore(1,',');
cout<<temp<<' ';
}
return 0;
}
どういうわけか私の出力はではありません1 2 5 6が49 50 53 54。何が得られますか?
アップデート:
また、の別の実装があることに気づきましたget()。私が定義すれば、私はchar tempそれを行うことができfile.get(temp)、それはまた私がASCII表現を変換することを節約するでしょう。しかし、私は使うのが好きなwhile (file >> temp)ので、それを使います。ありがとう。