【发布时间】:2013-02-24 00:58:34
【问题描述】:
我有以下 .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),所以我将继续使用它。谢谢。
【问题讨论】:
标签: c++ text-files iostream fstream cout