連絡先を追加するプログラムの方法の 1 つは、実行時に次のエラーが発生することです。
Unhandled exception at 0x00111deb in G00290342.exe: 0xC0000005: Access violation reading location 0x00000050.
エラーを調査したところ、既に使用されているメモリへのアクセスが原因であることがわかりました。「curr」変数が NULL になる原因となっている readFile メソッドのポインター ロジックに絞り込みました。
壊れているコードは次のとおりです。
while(curr->next != NULL)
{
curr = curr->next;
}
これは、参照用の完全な方法です。
int addContact(struct contact *theList)
{
struct contact *newContact, *curr;
char fn[15],sn[15],ph[15],cmpy[15],eml[15];
//create the new structure
newContact = (struct contact *)malloc(sizeof(struct contact));
if(newContact == NULL)
{
return(0);
}
//find the end of list
curr = theList;
//scroll through the list
while(curr->next != NULL)
{
curr = curr->next;
}
//now have the last contact and the new one here
printf("\nEnter a surname: ");
gets(newContact->sname);
printf("\nEnter a first name: ");
gets(newContact->fname);
printf("\nEnter a phone: ");
gets(newContact->phone);
printf("\nEnter a company: ");
gets(newContact->company);
printf("\nEnter an email: ");
gets(newContact->email);
//add the new contact to the end of the list
curr->next = newContact;
newContact->prev = curr;
newContact->next = NULL;
return(0);
}//end addContact
これは、テスト ファイルを読み取る主な方法です。
main()
{
int sts,iChoice;
struct contact *ptrList, *head;
//head of sorted list
struct contact *srtdList;
srtdList = NULL;
ptrList = NULL;
ptrList = readFile("test.csv",ptrList);
head = ptrList;
/////menu for options
system("cls");
printf("\n\n\t\tWelcome to BV Contact Organizer\n\n");
printf("\n\n\t\tEnter a number ranging from 1 to 6 for options.\n\n");
printf("\n\t\t1. Search");
printf("\n\t\t2. Add");
printf("\n\t\t3. Sort");
printf("\n\t\t4. Remove");
printf("\n\t\t5. Edit");
printf("\n\t\t6. Exit");
printf("\n\n\t\tEnter your menu choice: ");
fflush(stdin);
scanf("%d", &iChoice);
// user enters one of 6 values:
// Search,add,sort,remove,exit or edit contacts
switch(iChoice)
{
case 1: // Add
{
sts = addContact(head);
sts = writeListToFile("test.csv",head);
while(ptrList != NULL)
{
printf("\n%s,%s,%s,%s,%s",ptrList->sname,ptrList->fname,ptrList->phone,ptrList->company,ptrList->email);
ptrList = ptrList->next;
}
break;
}
case 2: // Sort
{
//return the head of the sorted list to here
srtdList = sortList(head,srtdList);
head = srtdList;
if(srtdList != NULL)
{
printf("\n\nSorted List");
while(srtdList != NULL)
{
printf("\n%s,%s,%s,%s,%s",srtdList->sname,srtdList->fname,srtdList->phone,srtdList->company,srtdList->email);
srtdList = srtdList->next;
}
sts = writeListToFile("testSort.csv",head);
}
else
{
printf("nothing to print");
}
printf("\n\n\n");
system("pause");
break;
}
case 3: // Exit
{
printf("\n\nProgram exiting!...");
break;
}
default:
{
printf("\n\nInvalid menu choice,please choose a number ranging from 1 to 6!...");
}
}//end of switch
return(iChoice);
} // end of main
これは、要求された struct contact の定義です。
struct contact {
char sname[15];
char fname[15];
char phone[15];
char company[15];
char email[15];
struct contact *prev;
struct contact *next;
};
これは readFile に使用されるメソッドであり、問題を引き起こしています。
struct contact *readFile(char * FName,struct contact *ptrList)
{
struct contact *head, *newContact;
FILE *fptr;
char oneLine[60];
char *sname, *fname, *phone,*company, *email;
head = ptrList;
fptr = fopen(FName,"r");
if(fptr == NULL)
{
printf("\nCant open file!");
return(ptrList);
}
fgets(oneLine, 55, fptr);
while(!feof(fptr))
{
fgets(oneLine, 55, fptr);
if(oneLine[strlen(oneLine)-1] == '\n')
{
oneLine[strlen(oneLine)-1] = '\0';
}
sname = strtok(oneLine,",");
fname = strtok(NULL,",");
phone = strtok(NULL,",");
company = strtok(NULL,",");
email = strtok(NULL,",");
if(head == NULL)
{
head = (struct contact *)malloc(sizeof(struct contact));
ptrList = head;
strcpy(head->sname,sname);
strcpy(head->fname,fname);
strcpy(head->phone,phone);
strcpy(head->company,company);
strcpy(head->email, email);
head->prev = NULL;
head->next = NULL;
}
else
{
newContact = (struct contact *)malloc(sizeof(struct contact));
head->next = newContact;
newContact->prev = head;
newContact->next = NULL;
//copy the data to the new one
strcpy(head->sname,sname);
strcpy(head->fname,fname);
strcpy(head->phone,phone);
strcpy(head->company,company);
strcpy(head->email,email);
//move down the list so that the head variable
//points to the last contact
head = newContact;
}
}//end while
fclose(fptr);
return(ptrList);
}