Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stddef.h>
- #include <float.h>
- // Visual Studio: to skip warning message for safe functions.
- // Else delete pragma.
- #pragma warning(disable:4996)
- float next_data(FILE* source);
- void append_info(FILE* source, FILE* dest);
- int main()
- {
- FILE* output_file = fopen("output.txt", "a");
- while (1)
- {
- char file_path[512];
- printf("\nEnter filepath to read values from or 'n' to quit: \n");
- scanf("%s", file_path);
- if (file_path[0] == 'n')
- break;
- FILE* source_file = fopen(file_path, "r");
- if (!source_file)
- {
- printf("\nNo such file!\n");
- break;
- }
- append_info(source_file, output_file);
- fclose(source_file);
- }
- fclose(output_file);
- return EXIT_SUCCESS;
- }
- float next_data(FILE* source)
- {
- float input = 0;
- fscanf(source, "%f", &input);
- return input;
- }
- void append_info(FILE* source, FILE* dest)
- {
- float min = FLT_MAX, max = -FLT_MAX; // no std::numeric_limits so fuck this language tbh
- float sum = 0, avg = 0;
- size_t elem_count = 0;
- while(!feof(source))
- {
- float curr = next_data(source);
- ++elem_count;
- if (curr < min)
- min = curr;
- if (curr > max)
- max = curr;
- sum += curr;
- }
- if (!elem_count)
- {
- fprintf(dest, "%s", "\nNo data.\n");
- return;
- }
- avg = sum / elem_count;
- fprintf(dest, "min: %f, max: %f, avg: %f\n", min, max, avg);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement