i have a few issues with that i was wondering what am i doing wrong and if can someone explain me futher details about the realloc and malloc functions. the problem i have is from my main:
typedef struct Books {
char *id;
char *title;
char *author;
char *pages;
char *year;
char *subject;
} book;
char* filename;
int bookcount=1;
int libsize=4;
int main(int argc, char* argv[]){
if (argc < 2)
return -1;
filename=argv[1];
FILE* fptr;
book* books;
char tempstring[maxsize],* token;
int i=0,ch;
fptr=fopen(filename,"r");
if(fptr==NULL)
return-1;
//this count how many books are in the file
while(ch!= EOF){
ch=fgetc(fptr);
if(ch == '\n')
++bookcount;
}
fclose(fptr);
while(libsize<bookcount){
libsize *= 1.5;
}
books=(book*) malloc(libsize*sizeof(book));
if(books==NULL)
exit(-1);
fptr=fopen(filename,"r");
if(fptr==NULL)
return-1;
//this gets all the books into the book array
for(i=0;i<bookcount;i++){
fgets(tempstring,maxsize,fptr);
token=strtok(tempstring,",");
ch=strlen(token);
books[i].id=(char*)malloc(ch+1);
strcpy(books[i].id,token);
token=strtok(NULL,",");
ch=strlen(token);
books[i].title=(char*)malloc(ch+1);
strcpy(books[i].title,token);
token=strtok(NULL,",");
ch=strlen(token);
books[i].author=(char*)malloc(ch+1);
strcpy(books[i].author,token);
token=strtok(NULL,",");
ch=strlen(token);
books[i].pages=(char*)malloc(ch+1);
strcpy(books[i].pages,token);
token=strtok(NULL,",");
ch=strlen(token);
books[i].year=(char*)malloc(ch+1);
strcpy(books[i].year,token);
token=strtok(NULL,",");
ch=strlen(token);
books[i].subject=(char*)malloc(ch+1);
strcpy(books[i].subject,token);
}
books=(book*) realloc(books,libsize*sizeof(book));
fclose(fptr);
printf("to add a book press 1\n");
printf("to delete a book press 2\n");
printf("to find a book press 3\n");
printf("to print all books press 4\n");
printf("to save library in a file press 5\n");
printf("to add books from a file press 6\n");
printf("to exit press 0\n");
pick(books);
free(books);
return 1;
}
i send an array of dynamicly allocated structs into a funtction that let me pick now when i call to print the books
void printbooks(book* books){
int i;
for(i=0;i<bookcount;++i){
printf("%s\n",books[i].title);
}
printf("Fin\n");
pick(books);
}
i get an expected output how ever when i use the function addbook
void addbook(book* books){
char tempstring[maxsize];
gets(tempstring);
book* temp;
int i=bookcount-1,ch;
++bookcount;
if(libsize < bookcount){
while(libsize < bookcount){
libsize*=1.5;}
temp=(book*)realloc(books,libsize);
}
if(temp==NULL){
printf("no more space\n");
exit(-1);}
if(temp!=NULL){
books=temp;}
printf("add the id\n");
gets(tempstring);
ch=strlen(tempstring);
books[i].id=(char*)malloc(ch+2);
strcpy(books[i].id,tempstring);
printf("add the title\n");
gets(tempstring);
ch=strlen(tempstring);
books[i].title=(char*)malloc(ch+2);
strcpy(books[i].title,tempstring);
printf("add the author\n");
ch=strlen(tempstring);
books[i].author=(char*)malloc(ch+2);
gets(tempstring);
strcpy(books[i].author,tempstring);
printf("add the pages\n");
gets(tempstring);
ch=strlen(tempstring);
books[i].pages=(char*)malloc(ch+2);
strcpy(books[i].pages,tempstring);
printf("add the year\n");
gets(tempstring);
ch=strlen(tempstring);
books[i].year=(char*)malloc(ch+2);
strcpy(books[i].year,tempstring);
printf("add the subject\n");
gets(tempstring);
ch=strlen(tempstring);
books[i].subject=(char*)malloc(ch+2);
strcpy(books[i].subject,tempstring);
printf("book number %d added",bookcount);
printf("\n");
pick(books);
}
some of the struct members gets corrupted and the program crash, also when i tried to change the library size to 20 and then run it once i tried to enter the first member of the new book the program crashed.