Advertisement
Guest User

load

a guest
Sep 23rd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. int *load(char *s, int features)
  4. {
  5.     FILE *f = fopen(s, "r");
  6.  
  7.     if(f == NULL)
  8.     {
  9.         printf("file reading error\n");
  10.         exit(1);
  11.     }
  12.  
  13.     int i, j, a, *x;
  14.     x = (int*)calloc(1000*10, sizeof(int));
  15.  
  16.     i = 0;
  17.     while(!feof(f))
  18.     {
  19.         for(j = 0; j < features; j++)
  20.         {
  21.             fscanf(f, "%d", &a);
  22.             x[features*i + j + 2] = a;
  23.         }
  24.  
  25.         i++;
  26.     }
  27.  
  28.     x[0] = i;
  29.     x[1] = features;
  30.  
  31.     fclose(f);
  32.  
  33.     return x;
  34. }
  35. int main()
  36. {
  37.     int i, j, *p = load("data.txt", 2);
  38.  
  39.     for(i = 0; i < p[0]; i++)
  40.     {
  41.         for(j = 0; j < p[1]; j++)
  42.             printf("%d ", p[i*p[1] + j + 2]);
  43.         printf("\n");
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement