fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. #define ALLOC_BLOCK_SIZE 4
  6.  
  7. int *readAllocMatrixLine(FILE *f, int *count) {
  8. static char buf[2048];
  9. int resultSize = 0;
  10. int *result = 0;
  11.  
  12. if (fgets(buf, sizeof(buf), f)) {
  13. char *p=buf;
  14. int resultAlloc = ALLOC_BLOCK_SIZE;
  15. result = (int *)calloc(resultAlloc, sizeof(int));
  16. if (result) {
  17. int x;
  18. while(1) {
  19. while(isspace(*p))
  20. ++p;
  21. if (*p==0)
  22. break;
  23. x = strtol(p, &p, 10);
  24. if (!p)
  25. break;
  26. if (resultSize==resultAlloc) {
  27. resultAlloc += ALLOC_BLOCK_SIZE;
  28. result = (int *)realloc(result, resultAlloc*sizeof(int));
  29. }
  30. result[resultSize] = x;
  31. resultSize++;
  32. }
  33. }
  34. }
  35. *count = resultSize;
  36. return result;
  37. }
  38.  
  39. int main(void) {
  40. while(!feof(stdin)) {
  41. int n, i;
  42. int *tab = readAllocMatrixLine(stdin, &n);
  43. printf("new tab (%4d): ", n);
  44. for(i=0; i<n; ++i)
  45. printf("%4d", tab[i]);
  46. printf("\n");
  47. free(tab);
  48. }
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0.01s 1856KB
stdin
1 2 3 4
1 2 3 4 -2
1 2 4 8 16 32 64 128
-1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
2
stdout
new tab (   4):    1   2   3   4
new tab (   5):    1   2   3   4  -2
new tab (   8):    1   2   4   8  16  32  64 128
new tab (  33):   -1   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32
new tab (   1):    2
new tab (   0):