从文件C ++读取数字 [英] Reading a number from file C++

查看:464
本文介绍了从文件C ++读取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单数字的档案,例如:

  66 

  126 

如何在C ++中读取 int / p>

请注意,该文件还可能包含一些空格或在之后输入



我开始这样:

  int ReadNumber()
{
fstream filestr;
filestr.open(number.txt,fstream :: in | fstream :: app);

filestr.close()
}



感谢。

解决方案

当他只想要输入时,人们使用fstream和设置标志。

  #include< fstream> 
using namespace std;

int main(){
ifstream fin(number.txt);
int num;
fin>> num;
return 0;
}


I have a file with a simple number, for example:

66

or

126

How can I read it to a int value in C++?

Note that the file may also contain some spaces or enters after the number.

I started like so:

int ReadNumber() 
{
    fstream filestr;
    filestr.open("number.txt", fstream::in | fstream::app);

    filestr.close()
}

How to continue?

Thanks.

解决方案

I don't really know why people are using fstream with set flags when he only wants to do input.

#include <fstream>
using namespace std;

int main() {
    ifstream fin("number.txt");
    int num;
    fin >> num;
    return 0;
}

这篇关于从文件C ++读取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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