fstream的数据输入不正确 [英] Incorrect data input with fstream

查看:173
本文介绍了fstream的数据输入不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用fstream从文本文件读取数据,但得到错误的数据。

I tried to read in data from a text file using fstream but got wrong data.

ifstream fin ("C:\\Users\\rEgonicS\\Documents\\test.in");
int number;
fin >> number;
cout << number;

test.in $ c> 12 。

cout 读取 4273190

test.in is simply 12.
cout reads 4273190.
Can someone explain why this is so and how to fix it?

推荐答案

最有可能的原因是,文件打开失败。打开后,以及阅读后检查状态;对于一个简单的测试,做这样的事情:

The most likely cause is that the file open failed. Check the status after opening, and also after reading; for a simple test, do something like this:

ifstream fin ("C:\\Users\\rEgonicS\\Documents\\test.in");
if (!fin) cout << "File open failed\n";
int number;
fin >> number;
if (!fin) cout << "File read failed\n";
cout << number;

这可能会对发生的事情提供进一步的线索。

This might give a further clue as to what's going on.

这篇关于fstream的数据输入不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆