Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define INITIAL_SIZE 3
- int main()
- {
- FILE *fp;
- int size = INITIAL_SIZE;
- int length,i, count =0;
- char **brand=NULL;
- char **model=NULL;
- int *year=NULL;
- brand = (char**)malloc(size*sizeof(char*));
- model = (char**) malloc(size*sizeof(char*));
- year = (int*) malloc(size*sizeof(int));
- for(i=0;i<size;i++)
- {
- brand[i] = (char*) malloc(40);
- model[i] = (char*) malloc(40);
- }
- if((fp=fopen("cars.bin", "rb")) == NULL)
- {
- printf("couldnt open file");
- exit(1);
- }
- while(1)
- {
- if(fread(&length, sizeof(int), 1, fp) != 1)
- {
- printf("1st length read end\n");
- break;
- }
- if(fread(brand[count], 1, length, fp) != length)
- {
- printf("brand read end\n");
- break;
- }
- if(fread(&length, sizeof(int), 1, fp) != 1)
- {
- printf("2nd length read end\n");
- break;
- }
- if(fread(model[count], 1, length, fp) != length)
- {
- printf("model read end\n");
- break;
- }
- if(fread(&year[count], sizeof(int),1, fp) != 1)
- {
- printf("year read end\n");
- break;
- }
- count++;
- if(count==size)
- {
- brand = realloc(brand, size*2*sizeof(char*));
- model = realloc(model, size*2*sizeof(char*));
- year = realloc(year, size*2*sizeof(int));
- if(model == NULL)
- {
- printf("model reallocate error\n");
- break;
- }
- if(brand == NULL)
- {
- printf("brand reallocate error\n");
- break;
- }
- if(year == NULL)
- {
- printf("year reallocate error\n");
- break;
- }
- for(i=size;i<2*size;i++)
- {
- brand[i] = (char*) malloc(40);
- model[i] = (char*) malloc(40);
- }
- size*=2;
- }
- }
- fclose(fp);
- for(i=0;i<count;i++)
- {
- printf("%s\n", brand[i]);
- printf("%s\n", model[i]);
- printf("%d\n", year[i]);
- }
- for(i=0;i<size;i++)
- {
- free(brand[i]) ;
- free(model[i]);
- }
- free(brand);
- free(model);
- free(year);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement