我有以下 .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
,而是 49 50 53 54
。给了什么?
更新:
此外,我注意到还有另一个 get()
的实现。如果我定义 char temp
那么我可以执行 file.get(temp)
并且这也将节省我转换 ASCII 表示的时间。但是我喜欢使用 while (file >> temp)
所以我会继续使用它。谢谢。
最佳答案
temp
是一个整数。因此,在将 char 转换为 int 后,您会看到编码后的 ascii 值。
关于c++ - 文件中的字符不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15047540/