我正在尝试将输出打印到C中的文件,但我的代码不是创建文件。有人可以帮我吗?

时间:2018-03-31 00:35:57

标签: c file-io

继承我的代码,它从一个名为input_data.txt的文件中获取输入(这只是一组数字)并且应该打印到输出文件但不是。如果我只是将它设为printf,它会给我正确的值集,但当我将其更改为fprintf时,没有任何反应。所有这一切都是用exitcode 0完成的。

#include <stdio.h>



int main() {

int month;
int day;
int hour = 0;
int minute = 0;
int station_num = 0;
float temp = 0;
int lines = 0;
int run_number = 0;
FILE *daily_averages_summary = fopen("input_data.txt", "r");

if (daily_averages_summary == NULL) {
    printf("Something went wrong!\n");
    return 1;
}


while (fscanf(daily_averages_summary,"%d %d %d %d %d %f",&month,&day,&hour,&minute,&station_num,&temp)==6) {

    int ch = getc(daily_averages_summary);

    if (ch == '\n') {
        lines++;
    }
}
    rewind(daily_averages_summary);

    float daily_tempsum[13][32];
    float daily_tempcount[13][32];
    float daily_avg[13][32];

    for (month = 0; month < 13; month++) {
        for (day = 0; day < 32; day++) {
            daily_tempsum[month][day] = 0;
            daily_tempcount[month][day] = 0;
            daily_avg[month][day] = 999;
        }
    }
    while (run_number <= lines) {

        fscanf(daily_averages_summary, "%d %d %d %d %d %f", &month, &day, &hour, &minute, &station_num, &temp);

        daily_tempsum[month][day] = daily_tempsum[month][day] + temp;
        daily_tempcount[month][day]++;
        run_number++;

    }
    fclose(daily_averages_summary);

    for (month = 0; month < 13; month++) {
        for (day = 0; day < 32; day++) {

            if (daily_tempsum[month][day] != 0) {
                daily_avg[month][day] = (daily_tempsum[month][day] / daily_tempcount[month][day]);
            }
        }
    }
    daily_averages_summary = fopen("input_data.txt", "r");

    for (month = 0; month < 13; month++) {
        for (day = 0; day < 32; day++) {

            if (daily_avg[month][day] != 999) {
                fprintf(daily_averages_summary, "%d %d %0.2f\n", month, day, daily_avg[month][day]);
            }
        }
    }

fclose(daily_averages_summary);
return 0;

}

0 个答案:

没有答案