struct contact
{
char name[20];
char email[20];
int hpnum;
}add;
FILE *f;
void addcontact(struct contact list[3]);
void savecontact(struct contact list[3]);
int main (void)
{
int option,i;
struct contact list[3];
do
{
system("cls");
printf("==========Welcome to Jeffery's Contact System Management==========\n");
printf("\t\t\tContact System Main Menu\n");
printf("[1] Create a New Contact\n");
printf("[2] Modified Existing Contact\n");
printf("[3] Delete Existing Contact\n");
printf("[4] Search Existing Contact\n");
printf("[5] Exit\n");
printf("Please enter one of your option.\n");
scanf("%d",&option);
switch(option)
{
//add new contact
case 1:addcontact(list);savecontact(list);
break;
case 2:
case 3:
case 4:for(i=0;i<3;i++)
{
if(list[i].email!=0){
printf("\nContact Name: %s",list[i].name);
printf("\nHandphone Number: %d",list[i].hpnum);
printf("\nE-mail: %s",list[i].email);
}
}
getch();
break;
case 5:exit(EXIT_SUCCESS);
}
}while(i=1);
getch();
}
void addcontact(struct contact list[3])
{
char name[20],email[20];
int hpnum,no=0;
fflush(stdin);
printf("\nContact Name: ");
scanf("%s",list[no].name);
fflush(stdin);
printf("\nHandphone Number: ");
scanf("%d",&list[no].hpnum);
fflush(stdin);
printf("\nE-mail: ");
scanf("%s",list[no].email);
}
void savecontact(struct contact list[3])
{
FILE *f;
f=fopen("contact.txt","w");
fwrite(list,sizeof(list),3,f);
fclose(f);
}
我添加联系人之后,当我在cmd内部搜索时,它会存储它。当我搜索联系人时,它包括搜索联系人部分中的奇怪单词。此外,它执行的txt文件,在我写的内容中没有任何内容。它只是一个奇怪的词。
答案 0 :(得分:2)
您正在接受contact
函数中的单个addcontact
并在savecontact
函数中保存3个对象。
您的addcontact
功能应该是这样的
void addcontact(struct contact *list)
{
char name[20],email[20];
int hpnum,no=0;
fflush(stdout);
printf("\nContact Name: ");
scanf("%s",list->name);
fflush(stdout);
printf("\nHandphone Number: ");
scanf("%d",&list->hpnum);
fflush(stdout);
printf("\nE-mail: ");
scanf("%s",list->email);
}
像这样称呼它
struct contact list;
addcontact(&list);
答案 1 :(得分:1)
fflush(stdin);
出现undefined错误。应该使用fflush(stdout);
savecontact()
函数fwrite(list,sizeof(list),3,f);
中的错误。它应该是
fwrite(list, sizeof(struct contact), 3, f);
声明甚至错误的空虚addcontact(struct contact list[3])
应为
void addcontact(struct contact *list)
编辑
更正了您的代码。请阅读我在您的代码中添加的评论。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct contact
{
char name[20];
char email[20];
int hpnum;
}add;
FILE *f;
void addcontact(struct contact list[3]);
void savecontact(struct contact list[3]);
int main (void){
int option ;
struct contact list[1]; // chnages the size of Lint to One
do
{
printf("==========Welcome to Jeffery's Contact System Management==========\n");
printf("\t\t\tContact System Main Menu\n");
printf("[1] Create a New Contact\n");
printf("[2] Modified Existing Contact\n");
printf("[3] Delete Existing Contact\n");
printf("[4] Search Existing Contact\n");
printf("[5] Exit\n");
printf("Please enter one of your option.\n");
scanf("%d",&option);
switch(option)
{
//add new contact
case 1:
addcontact(list);
savecontact(list);
break;
case 2:
case 3:
case 4: // read from file instead from memroy (list)
f=fopen("contact.txt","rb");
while(fread(list,sizeof(struct contact),3,f)>0)
printf("%s %d %s\n",list[0].name, list[0].hpnum, list[0].email);
fclose(f);
break;
case 5:exit(EXIT_SUCCESS);
}
}while(1); // changed to 1 instead i=1 and remove i variable from declaration
}
void addcontact(struct contact *list) // Declaration corrected
{
int no=0;
fflush(stdout); // fflush(stdin); was wrong! (this line not needed here)
printf("\nContact Name: ");
scanf("%s",list[no].name);
fflush(stdout);
printf("\nHandphone Number: ");
scanf("%d",&list[no].hpnum);
fflush(stdout);
printf("\nE-mail: ");
scanf("%s",list[no].email);
}
void savecontact(struct contact *list) // Declaration corrected
{
// remove redeclaration of `File* f`
f=fopen("contact.txt","ab"); // open file in a+ mode instead w
fseek(f, 0, SEEK_END); // Added this line to shift to end of file
fwrite(list,sizeof(struct contact),3,f); //Corrected this line
fclose(f);
}
它的工作,A run:
:~$ gcc c.c -Wall
:~$ ./a.out
==========Welcome to Jeffery's Contact System Management==========
Contact System Main Menu
[1] Create a New Contact
[2] Modified Existing Contact
[3] Delete Existing Contact
[4] Search Existing Contact
[5] Exit
Please enter one of your option.
1
Contact Name: grijesh
Handphone Number: 123
E-mail: g@123
==========Welcome to Jeffery's Contact System Management==========
Contact System Main Menu
[1] Create a New Contact
[2] Modified Existing Contact
[3] Delete Existing Contact
[4] Search Existing Contact
[5] Exit
Please enter one of your option.
1
Contact Name: yourname
Handphone Number: 1234
E-mail: your@email
==========Welcome to Jeffery's Contact System Management==========
Contact System Main Menu
[1] Create a New Contact
[2] Modified Existing Contact
[3] Delete Existing Contact
[4] Search Existing Contact
[5] Exit
Please enter one of your option.
4
grijesh 123 g@123
yourname 1234 your@email