gpt4 book ai didi

c++ - C编程: Running 2 while loops and get the random result for data from loop1

转载 作者:行者123 更新时间:2023-11-30 19:52:28 26 4
gpt4 key购买 nike

我使用 2 个 while 循环从 2 个文件读取数据并存储在 2 个不同的数组中。问题是当我想打印第一个数组中的数据或在第二个 while 循环之后打印第一个 while 循环中的数据时,我得到的随机结果与文件中出现的不同。

示例1.txt

4.562
5.758
9.865
10.235
12.575

示例2.txt

15.756
17.698
56.689
62.145
#include <stdio.h>

int main(){
FILE *fp, *fp2;

fp = fopen("example1.txt", "r");
fp2 = fopen("example2.txt", "r");
float breaks_buffer[255], yr_buffer[1000], res1[200];
float new_data_breaks[200], new_data_yr[200];
int i, new_index_breaks, j, new_index_yr, a;

/*=========== GET DATA FROM FILES INTO ARRAYS ==========*/

/*=== DATA_BREAKS INTO ARRAY: "new_data_breaks" ===*/
i=0;
new_index_breaks =0;
while(!feof(fp)){
fscanf(fp, "%f", &breaks_buffer[i]);
new_data_breaks[new_index_breaks] = breaks_buffer[i];
i++;
new_index_breaks++;
}
/*=== DATA_YR INTO ARRAY: "new_data_yr" ===*/
j=0;
new_index_yr =0;
while(!feof(fp2)){
fscanf(fp2, "%f", &yr_buffer[j]);
new_data_yr[new_index_yr] = yr_buffer[j];
j++;
new_index_yr++;
}

/*=== TEST PRINT DATA ===*/
for(a=0; a<new_index_breaks;a++){
printf("%f\n",new_data_breaks[a]);
}
fclose(fp);
fclose(fp2);
return 0;
}

最佳答案

尝试一下while(fscanf(~~~) != EOF) {...}

缓冲区数组真的需要吗?我宁愿不使用这些在我的情况下。祝你好运。

关于c++ - C编程: Running 2 while loops and get the random result for data from loop1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57049394/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com