Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define INITIAL_SIZE 5
- void readTextFile(char **brand, char **model, int *year);
- int main()
- {
- char **brand, **model;
- int *year;
- int i, index;
- brand=(char**)malloc(sizeof(int)*INITIAL_SIZE);
- model=(char**)malloc(sizeof(int)*INITIAL_SIZE);
- year=(int*)malloc(sizeof(int)*INITIAL_SIZE);
- readTextFile(brand, model, year);
- for(i=0;i<index;i++)
- {
- free(brand[i]);
- }
- for(i=0;i<index;i++)
- {
- free(model[i]);
- }
- free(brand);
- free(model);
- free(year);
- return 0;
- }
- void readTextFile(char **brand, char **model, int *year)
- {
- FILE *fp;
- int index=0;
- int i;
- int size = INITIAL_SIZE;
- if((fp=fopen("zad1.txt","r"))==NULL) exit(1);
- while(!feof(fp))
- {
- brand[index] = (char*) malloc(40);
- fscanf(fp,"%s",brand[index]);
- model[index] = (char*) malloc(40);
- fscanf(fp,"%s",model[index]);
- fscanf(fp,"%d",&year[index]);
- if(index==size)
- {
- brand = realloc(brand, 2*size*sizeof(char*));
- model= realloc(model, 2*size*sizeof(char*));
- year = realloc(year, 2*size*sizeof(int));
- size*=2;
- }
- }
- fclose(fp);
- for(i=0;i<index;i++)
- {
- printf("%s\t %s\t %d\n",brand[i], model[i], year[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement