使用C ++ ifstream从文本文件读取整数
这是使用C++ifstream从文本文件读取整数的示例。
示例
#include <fstream>
#include<iostream>
using namespace std;
int main() {
//初始化数组大小
int arr[30];
ifstream is("a.txt");
int cnt= 0;
int x;
//检查该数组是否尚未满
while (cnt < arr[30] && is >> x)
//并从文件中读取整数
arr[cnt++] = x;
//打印存储在数组中的整数
cout<<"整数是:"<<"\n";
for (int i = 0; i < cnt; i++) {
cout << arr[i] <<' ';
}
//关闭文件
is.close();
}输出结果
整数是: 1 2 3 4 5 6 7
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短