C++逐行读取txt中的数据,怎么保存并到数组中?

如题所述

第1个回答  推荐于2019-10-14
刚刚写了个,自己看看吧,应该看得明白。
#include <fstream>
#include<iostream>
using namespace std;

void main()
{
fstream file;
file.open("s.txt",ios::in);
if(!file)
cout<<"file not founded"<<endl;
int a [100];
int pos = 0;
while(!file.eof())//是否到文件结尾
{
file>>a[pos];
pos++;
if(pos>=100)
break;
}
file.close();
for(int i = 0;i<pos;i++)
cout<<a[i]<<endl;
}本回答被网友采纳
相似回答