數據如下:
0.00 882.197
8.35 877.375
15.66 871.794
27.24 864.176
37.27 855.956
41.28 852.139
43.72 848.769
55.84 838.139
71.62 825.459
82.78 820.639
98.28 817.389
109.84 817.409
125.86 817.949
136.84 817.469
152.48 817.829
163.08 816.759
179.03 815.589
191.22 814.779
206.42 810.829
218.68 809.769
234.01 809.919
245.20 816.699
261.88 817.219
272.39 829.269
288.68 840.729
308.09 852.139
328.05 866.150
342.80 878.883
354.90 885.711
363.64 890.659
372.71 893.685
用ifstream和sscanf來實現
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
float arr1[64];
float arr2[64];
int i = 0;
ifstream myfile("F:\\test.txt");
if(!myfile){
cout << "Unable to open myfile";
exit(1); // terminate with error
}
else
{
char str[64] = {0};
while (!myfile.eof())
{
myfile.getline (str, 64); //讀取一行數據
sscanf(str, "%f %f", &arr1[i], &arr2[i]);
i++;
}
}
//打印兩個一維數組
for(int j = 0; j < i; j++)
cout << arr1[j] << " " << arr2[j] << endl;
}