После помещения значений в базу данных в массив и отправки значений в addReader
внутри самой функции значения сохраняются успешно, однако при возврате к основному значению введенные значения исчезают.
С учетом следующего кода: создание динамического массива с 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;
}
С учетом вызывающей функции:
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
}
С учетом вызова функции в main:
rdr=addReader(rdr,readNum);
Определение структуры читателя:
typedef struct reader{
int numToTake;
char *first_name, *last_name, *address, *phone, *id;
}reader;
Что я делаю не так?С уважением, Дэвид
РЕДАКТИРОВАТЬ!
Все еще не работает.Новая редакция кода:
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
}
Новое призвание в основном:
addReader(&rdr,readNum);