There are 600 files in a folder, each file is a list of 13 columns: year, month, day, temperature, and the remaining 9 data columns. The task is to find the average annual temperature for each file (i.e. for each object) and output these values to another file. To find the average annual temperature for each individual file, I wrote a program, here it is:
#include <stdio.h> #include <stdlib.h> #include <iostream> // 1946 12 30 0 -519 0 -490 0 9999 9 0 2 0 typedef struct { int year; int month; int day; int param4; int param5; int param6; int t; int param8; int param9; int param10; int param11; int param12; int param13; } FILE_DATA; int l = 1; int sum = 1; int k = 1; double arifmet = 0; double arifmet2 = 0; int current_sum = 0; int current_year = 0; int main() { int i; // Счётчик int n = 0; // Будет содержать число ситанных строк FILE_DATA *pList = NULL; // Указатель на массив структур FILE_DATA FILE *file = fopen("U24763", "rb"); // Инициализируем поток чтения FILE *file2 = fopen("input2.txt", "w"); FILE *file3 = fopen("Srednee_Zna4enie_temperatur", "w"); if (file == NULL) printf("Error open input.txtn"); else { // Выделяем память под первую структуру массива pList = (FILE_DATA *) malloc(sizeof(FILE_DATA)); for (n = 0; !feof(file);) { // Мало кто знает, но fscanf возвращает // число считанных параметров if (13 == // Как раз проверяем все ли 13 параметров считали fscanf(file, "%d %d %d %d %d %d %d %d %d %d %d %d %dn", &pList[n].year, &pList[n].month, &pList[n].day, &pList[n].param4, &pList[n].param5, &pList[n].param6, &pList[n].t, &pList[n].param8, &pList[n].param9, &pList[n].param10, &pList[n].param11, &pList[n].param12, &pList[n].param13) ) { // Если чтение успешно печатает n-ую структуру printf("%04d %02d %02d %d %d %d %d %d %d %d %d %d %dn", pList[n].year, pList[n].month, pList[n].day, pList[n].param4, pList[n].param5, pList[n].param6, pList[n].t, pList[n].param8, pList[n].param9, pList[n].param10, pList[n].param11, pList[n].param12, pList[n].param13); if (pList[n].t != 9999) { if (pList[n].year != current_year) { fprintf(file2, "%04d %d %d %.4f n", current_year, current_sum, l, arifmet); current_sum = 0; l = 1; current_year = pList[n].year; } else { current_sum += pList[n].t; l++; } sum += pList[n].t; k++; arifmet = current_sum / l; arifmet2 = sum / k; fprintf(file3, "%.4f %d %d", arifmet2, sum, k); } n++; // увеличиваем счётчик структур pList = (FILE_DATA *) realloc // Увеличиваем память под массив структур ((void *) pList, (1 + n) * sizeof(FILE_DATA) // одновременно ); } } // fwrite(file2,arifmet); fclose(file); fclose(file2); fclose(file3); if (n == 0) printf("File input.txt not contain correct datan"); else { printf("Totlal count of read structures %dn", n); // здесь у нас массив структур из n элементов // далее делам с ним что душе угодно } } printf("Enter any key for exitn"); getchar(); return 0; }
Question: how to add it to the files do not manually enter?