Advertisement
Kosty_Fomin

Untitled

Nov 28th, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <sys/stat.h>
  5.  
  6. struct answers_1{
  7.     long long sum;
  8.     long long b;
  9. };
  10.  
  11. struct answers_1  get_file_name(char *path);
  12.  
  13.  
  14. int main (void)
  15. {
  16.     struct answers_1  answers;
  17.     char* path = (char*) malloc( 100 );
  18.     printf("Input file name:\n");
  19.     scanf("%s",path);
  20.    
  21.     answers = get_file_name(path);
  22.    
  23.    
  24.     printf("Sum =  %lld ", answers.sum);
  25.    
  26. }
  27.  
  28.  
  29.  
  30.  
  31. struct answers_1  get_file_name(char *path){
  32.    
  33.     struct answers_1  answersf;
  34.     answersf.b = 0;
  35.     answersf.sum = 0;
  36.    
  37.     FILE *in;
  38.     in = fopen(path, "r");
  39.    
  40.     if (in == NULL) {
  41.         exit(1);
  42.     }
  43.    
  44.     long long buf=0;
  45.    
  46.    
  47.     while ((fscanf(in, "%lld", &buf)!=EOF)) {
  48.         answersf.sum+=buf;
  49.         answersf.b++;
  50.     }
  51.     answersf.sum = answersf.sum/answersf.b;
  52.     fclose(in);
  53.     return answersf;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement