数据如下:
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

c++从一两列文件按列读取数据到两一维数组中
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- 小灸舞 2016-04-23 03:51关注
用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; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报