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?

  • @ kuziashag85, According to the rules of the forum, questions should not be reduced to proposals to do the work. - Alexey Lobanov
  • In general, Boost has something that should suit you: solarix.ru/for_developers/cpp/boost/filesystem/ru/index.shtml - Alexey Lobanov
  • one
    If you intend to regularly engage in the processing of data of this kind, I recommend mastering some high-level scripting language, such as Python or Perl. Programs will be written several times faster and without boiling brain. And use C for all sorts of complex and lengthy calculations. I speak from my own experience. - skegg pm
  • And yet: is it by chance a meteorological data base originating from Obninsk? - skegg
  • mikillskegg, maybe yes, maybe no. I have no idea =) My task was to sort the data, and then build isolines in mapinfo with a friend. Because Comrade understands programming as I do in ballet, I have to figure it out myself =) - kuziashag85

1 answer 1

@ kuziashag85 , you probably already solved your problem. If not, just use sh (or another scripting language) to call your program for each file.

It is not very clear why printing and output to input2 is needed. If this is not necessary, then remake the program a bit (reading the file from stdin, writing to stdout).

Then on sh you can write a very simple script (for example)

 mkdir ../results; for i in *; do your_prog <$i >../results/$i-sum; done 

And you will get your annual files in the ../resuls directory