我有以下 .txt 文件:
测试.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
but 49 50 53 54
。是什么赋予了?
更新:
另外,我注意到还有另一种实现get()
。如果我定义了char temp
,那么我可以这样做file.get(temp)
,这也将节省我转换 ASCII 表示的时间。但是我喜欢使用while (file >> temp)
,所以我会继续使用。谢谢。