주소록 용 프로그램을 작성 중입니다. 삽입, 표시 및 삭제 옵션이 있습니다. 삽입시 입력 데이터를 가져 와서 파일에 저장합니다. 새 연락처를 추가 할 때마다 파일에 추가됩니다. 데이터를 파일에 저장 한 후 동적으로 구조체 주소록 배열을 할당하여 각 연락처 세부 정보를 저장할 수 있습니까? 따라서 특정 연락처를 표시하거나 삭제하려는 경우 파일을 여는 것 외에 파일의 각 요소를 비교하는 것 외에는 쉽지 않습니다. 파일에 저장된 연락처 수에 따라 구조체 주소록에 동적으로 배열을 할당하고 세부 정보를 저장할 수 있습니까?구조 배열을 동적으로 만들고 파일에 데이터를 저장하는 방법
#define FIRST_NAME_LENGTH 15
#define LAST_NAME_LENGTH 15
#define NUMBER_LENGTH 15
#define ADDRESS_LENGTH 15
/* Structure defining a name */
struct Name
{
char lastname[LAST_NAME_LENGTH];
char firstname[FIRST_NAME_LENGTH];
};
/* Structure defining a phone record */
struct addressbook
{
char answer;
struct Name name;
char address[ADDRESS_LENGTH];
char phonenumber[NUMBER_LENGTH];
};
struct addressbook a;
void add_record()
{
printf("enter details\n");
printf("enter lastname of person :\n");
scanf("%s", a.name.lastname);
printf("enter firstname of person :\n");
scanf("%s", a.name.firstname);
printf("enter address of person :\n");
scanf("%s", a.address);
printf("enter phone number of person :\n");
scanf("%s", a.phonenumber);
if((fp = fopen(filename,"a+")) == NULL){
printf("Error opening %s for writing. Program terminated.\n", filename);
abort();
}
fwrite(&a, sizeof(a), 1, fp); /* Write to the file */
fclose(fp); /* close file */
printf("New record added\n");
}