使用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 红包祝福语简短10字
18 周六早晨祝福语简短