Advertisement
cd62131

Count Numbers

Apr 25th, 2018
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.50 KB | None | 0 0
  1. #include <errno.h>
  2. #include <inttypes.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. static uint32_t count(FILE *in) {
  7.   uint32_t c = 0;
  8.   for (;;) {
  9.     errno = 0;
  10.     int x;
  11.     int ret = fscanf(in, "%d", &x);
  12.     if (ret == EOF || !ret || errno) {
  13.       break;
  14.     }
  15.     ++c;
  16.   }
  17.   return c;
  18. }
  19.  
  20. int main(void) {
  21.   FILE *in = fopen("data1.txt", "r");
  22.   if(!in) {
  23.     exit(1);
  24.   }
  25.   uint32_t c = count(in);
  26.   fclose(in);
  27.   printf("%"PRIu32"\n", c);
  28.   return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement