Advertisement
Mazamin

Sum From File With Random Strings

Dec 17th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAX_LEN 32
  4.  
  5. main(){
  6.     int sum=0, n;
  7.     char string[MAX_LEN];
  8.     FILE * fp, * fo;
  9.     ((fp=fopen("file.txt", "r"))!=NULL)?:(exit(1));
  10.     ((fo=fopen("output.txt", "w"))!=NULL)?:(exit(1));
  11.     while(feof(fp)==0)
  12.         if((fscanf(fp, "%d", &n))==1){
  13.             sum+=n;
  14.             fprintf(fo, "%d\t", n);
  15.         }
  16.         else
  17.             fscanf(fp, "%s", string);
  18.     fprintf(fo, "\nLa somma e' %d\n", sum);
  19.     fclose(fp);
  20.     fclose(fo);
  21.     return 0;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement