After puting the values in data base in an array and sending the values to addReader within the function itself ,the values are stored successfully , however when going back to main the entered values are gone.
Given the following code : creating the dynamic array with realloc
reader* readerBuilder(reader *rdr, int *readNum){ //building all the readers
FILE *read;
int i=1;
char *str;
read = fopen("Readers.txt","r");
checkFile(read);
str = readFromFile(read);
while(str != NULL)
{
rdr[i-1] = *cutToRdr(str);
str = readFromFile(read);
if(str != NULL){
i++;
rdr = (reader*)realloc(rdr,sizeof(reader)*i);
checkreader(rdr);
}
}
fclose(read);
*readNum = i;
return rdr;
}
Given the calling function :
reader* addReader(reader *rdr, int *readNum){ //adding a reader from the user
char string[1000];
rdr = (reader*)realloc(rdr,sizeof(reader)*(*readNum+1));// build new struct array ( bigger), with the old values
checkreader(rdr);// check if can get memory
printf("please enter the I.D. of the student you want to add:\n");
scanf("%s",string);// get from operant
rdr[*readNum].id=cutToStr(string);// put id in struct
printf("please add the reader's first name:\n");
scanf("%s",string);// get from operant
rdr[*readNum].first_name=cutToStr(string);// put first name in struct
printf("please add the reader's last name:\n");
scanf("%s",string);// get from operant
rdr[*readNum].last_name=cutToStr(string);// put last name in struct
printf("please add the reader's address:\n");
scanf("%s",string);// get from operant
rdr[*readNum].address=cutToStr(string);// put adress in struct
printf("please add the reader's phone:\n");
scanf("%s",string);// get from operant
rdr[*readNum].phone=cutToStr(string);// put phone in struct
rdr[*readNum].numToTake = 5;// change value of numbers to tke to 5
*readNum = *readNum + 1;// rise the number of the readers
return rdr;// return the new structs array
}
Given the calling for function in main:
rdr=addReader(rdr,readNum);
Defenition of the reader struct :
typedef struct reader{
int numToTake;
char *first_name, *last_name, *address, *phone, *id;
}reader;
What am i doing wrong ? Regards,David
EDIT!
Still doesn't work. The new edition of the code :
void addReader(reader **rdr, int *readNum){ //adding a reader from the user
char string[1000];
rdr = (reader**)realloc(rdr,sizeof(reader*)*(*readNum+1));// build new struct array ( bigger), with the old values
checkreader(*rdr);// check if can get memory
printf("please enter the I.D. of the student you want to add:\n");
scanf("%s",string);// get from operant
rdr[*readNum]->id=cutToStr(string);// put id in struct
printf("please add the reader's first name:\n");
scanf("%s",string);// get from operant
rdr[*readNum]->first_name=cutToStr(string);// put first name in struct
printf("please add the reader's last name:\n");
scanf("%s",string);// get from operant
rdr[*readNum]->last_name=cutToStr(string);// put last name in struct
printf("please add the reader's address:\n");
scanf("%s",string);// get from operant
rdr[*readNum]->address=cutToStr(string);// put adress in struct
printf("please add the reader's phone:\n");
scanf("%s",string);// get from operant
rdr[*readNum]->phone=cutToStr(string);// put phone in struct
rdr[*readNum]->numToTake = 5;// change value of numbers to tke to 5
*readNum = *readNum + 1;// rise the number of the readers
//return rdr;// return the new structs array
}
The new calling in main :
addReader(&rdr,readNum);
cutToStrdo? If it returns a pointer to the same string, then this string will be overwritten again and again. And isreaderBuildereven used? and how isrdrpassed toaddReaderinitialized?