Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #define INITIAL_SIZE 5
  3. void readTextFile(char **brand, char **model, int *year);
  4. int main()
  5. {
  6.     char **brand, **model;
  7.     int *year;
  8.     int i, index;
  9.     brand=(char**)malloc(sizeof(int)*INITIAL_SIZE);
  10.     model=(char**)malloc(sizeof(int)*INITIAL_SIZE);
  11.     year=(int*)malloc(sizeof(int)*INITIAL_SIZE);
  12.     readTextFile(brand, model, year);
  13.     for(i=0;i<index;i++)
  14.     {
  15.         free(brand[i]);
  16.     }
  17.     for(i=0;i<index;i++)
  18.     {
  19.         free(model[i]);
  20.     }
  21.     free(brand);
  22.     free(model);
  23.     free(year);
  24.     return 0;
  25. }
  26. void readTextFile(char **brand, char **model, int *year)
  27. {
  28.     FILE *fp;
  29.     int index=0;
  30.     int i;
  31.     int size = INITIAL_SIZE;
  32.     if((fp=fopen("zad1.txt","r"))==NULL) exit(1);
  33.     while(!feof(fp))
  34.     {
  35.         brand[index] = (char*) malloc(40);
  36.         fscanf(fp,"%s",brand[index]);
  37.         model[index] = (char*) malloc(40);
  38.         fscanf(fp,"%s",model[index]);
  39.         fscanf(fp,"%d",&year[index]);
  40.         if(index==size)
  41.         {
  42.              brand = realloc(brand, 2*size*sizeof(char*));
  43.              model= realloc(model, 2*size*sizeof(char*));
  44.              year = realloc(year, 2*size*sizeof(int));
  45.              size*=2;
  46.         }
  47.     }
  48.     fclose(fp);
  49.     for(i=0;i<index;i++)
  50.     {
  51.         printf("%s\t %s\t %d\n",brand[i], model[i], year[i]);
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement