我的地址簿存在轻微问题,如果我尝试加载联系人数据而未输入并保存至少一个联系信息,程序将返回“0”并冻结。另外,我觉得我需要在这个程序中添加一个删除联系人功能,如果有人能帮助我,那将非常感激= D。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define NAME 40
#define LNAME 40
#define ADDRESS 100
#define PCODE 6
#define PNUM 10
#define MAX 10
void Insert();
void Display();
void Search();
void Save();
void Load();
void Exit();
char temp [10];
typedef struct contact //defines the structure for our address book
{
char name [NAME ];
char Lname [LNAME ];
char address[ADDRESS];
char Pcode [PCODE];
char Pnum [PNUM];
}
contact;
int counter = 0;
int placeHolder = 0;
int loadContact;
int fileCount;
int save = 0;
FILE *PFileOut;
contact arrayContact [MAX];
int main()
{
int option;
char filechar;
do
{
printf("\n***Personal Contact Book V1.0***\n\n");
printf("1.Add new contact\n");
printf("2.Display current contacts\n");
printf("3.Search for a contact\n");
printf("4.Save contacts to file\n");
printf("5.Load contacts to file\n");
printf("6.Exit\n\n");
printf("> ");
scanf("%d", &option);
switch (option)
{
case 1:
Insert();
break;
case 2:
Display();
break;
case 3:
Search();
break;
case 4:
Save();
break;
case 5:
Load();
break;
case 6:
Exit();
break;
default:
printf("That is not a valid input, please choose between (1-6)");
}
}
while(option !=6 );
}
void Insert()
{
char option;
if(placeHolder>=10){
printf("Your contact list is full!");
return;
}
do{
printf("Contact Number: %d\n", counter+1);
printf("First name: ");
scanf(" %[^\n]s", arrayContact[counter].name);
printf("Last name: ");
scanf(" %[^\n]s", arrayContact[counter].Lname);
printf("Address: ");
scanf(" %[^\n]s", arrayContact[counter].address);
printf("Postal Code: ");
scanf(" %[^\n]s", arrayContact[counter].Pcode);
printf("Phone: ");
scanf(" %[^\n]s", arrayContact[counter].Pnum);
placeHolder++;
counter++;
printf("Press y/Y if you wish to add another contact, any key to return to main menu\n");
scanf(" %c",&option);
printf("\n");
}while( (option =='y'|| option == 'Y')&& placeHolder<MAX);
}
void Display()
{
counter = 0;
while(counter<placeHolder){
printf("Contact Number: %d\nFirst Name: %s\nLast Name: %s\nAddress: %s\nPostal Code: %s\nPhone: %s\n\n",counter+1,arrayContact[counter].name,
arrayContact[counter].Lname,arrayContact[counter].address,arrayContact[counter].Pcode,arrayContact[counter].Pnum);
counter++;
}
counter = placeHolder;
}
void Search()
{
char search [40];
char compare [40];
int counterLetter;
int counterContact;
int verify = 0;
printf("What is the contact's Last name? ");
scanf(" %s", search);
for( counterLetter = 0; search[ counterLetter ] != '\0'; counterLetter++ ) {
search[ counterLetter ] = tolower( search[ counterLetter ] );
}
for( counterContact = 0; counterContact<placeHolder ; counterContact++ ){
strcpy(compare,arrayContact[counterContact].Lname);
for( counterLetter = 0; compare[ counterLetter ] != '\0'; counterLetter++ ) {
compare[ counterLetter ] = tolower( compare[ counterLetter ] );
}
if( strcmp( search, compare ) == 0 ){
printf("Contact Number: %d\nFirst Name: %s\nLast Name: %s\nAddress: %s\nPostal Code: %s\nPhone: %s\n\n",counterContact+1,
arrayContact[counterContact].name,arrayContact[counterContact].Lname,arrayContact[counterContact].address,arrayContact[counterContact].Pcode,
arrayContact[counterContact].Pnum);
verify = 1;
}
}
if(verify == 0){
printf("No results found");
}
}
void Save()
{
PFileOut = fopen("Contact Book.dat","w");
fprintf(PFileOut,"%d\n",placeHolder);
for(counter = 0;counter<placeHolder;counter++){
fprintf(PFileOut,"\n%s\n%s\n%s\n%s\n%s",arrayContact[counter].name,arrayContact[counter].Lname,arrayContact[counter].address,arrayContact[counter].Pcode,
arrayContact[counter].Pnum);
}
fclose(PFileOut);
save = 1;
printf("\nSuccessfully Saved!!\n");
}
void Load()
{
char temp[40];
PFileOut = fopen("Contact Book.dat","r");
fscanf(PFileOut, "%d", &fileCount);
printf("%d\n", fileCount);
while(!feof(PFileOut)){
fscanf(PFileOut,"%s",&arrayContact[placeHolder].name);
fscanf(PFileOut,"%s",&arrayContact[placeHolder].Lname);
fscanf(PFileOut," %[^\n]s",&arrayContact[placeHolder].address);
fscanf(PFileOut,"%s",&arrayContact[placeHolder].Pcode);
fscanf(PFileOut, " %[^\n]s",&arrayContact[placeHolder].Pnum);
placeHolder++;
}
fclose(PFileOut);
}
void Exit()
{ char option6;
while(save!=1){
printf("It seems you have not saved your progress. Would you like to save? (y/n)");
scanf(" %c", &option6);
if(option6 == 'y'|| option6 == 'Y')
{
Save();
}
else
{
puts ("\nThank you for using Contact Book");
exit(0);
}
}
puts ("\nThank you for using Contact Book");
exit(0);
}
答案 0 :(得分:0)
您的程序存在很多问题。
如果.dat文件不存在,则fopen将失败,文件指针保持为NULL。 随后的fscanf也不起作用,这就是为什么你的记录数为0。带有NULL文件指针的feof的行为是未定义的,因此可能是您挂起的位置。根据所有权利,该程序应该崩溃。