目的:读取data.txt文件中的数据,并将其赋值给数组输出;
data.txt
1 -4 0 -1
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
int main () {
double ff;
int m;
double w[256] = {};
ifstream in("/home/song/Project1/data2.txt");
if (! in.is_open())
{ cout << "Error opening file"; exit (1); }
while (!in.eof() )
{
in>>ff;
w[m] = ff;
m++;
//in.getline (buffer,100);
}
for(int i=0;i<4;i++){
cout.width(5);
cout << w[i] <<endl;
return 0;
}
输出结果
1
-4
0
-1
2 4
1 3
3 0
2 -1
0 3
1 2