回 帖 发 新 帖 刷新版面

主题:程序未达到预期效果

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct person
{
char name[8];
char tel[15];
char qq[12];
char schpro[50];
char addr[20];
};
char filename[23]="E:\\我的通讯录.txt";
FILE *fp;
void creat();
void output();
void search();
void append();
void modify();
void del();
void main(void)

{
int m;
creat();   
while(1)   
{
printf("\n\t\t*********欢迎使用通讯录信息管理系统*********\n\n");
printf("\n\t\t添加,请按1 查找,请按2");
printf("\n\t\t修改,请按3 删除,请按4");
printf("\n\t\t输出,请按5 退出,请按0");
printf("\n\t\t\n");
printf("\n\t\t********************************************\n\n");
printf("请选择(0--5):");
scanf("%d",&m);
if(m>=0&&m<=5)
{
switch(m)
{
case 1: append();
break;
case 2: search();
break;
case 3: modify();
break;
case 4: del();
break;
case 5: output();
break;
case 0: exit(1);
}
printf("\n\n操作完毕,请再次选择!");
}
else
printf("\n\n选择错误,请再次选择!");
}
}

/*输入模块creat( ): 创建通讯录的子函数。*/
void creat()
{
struct person one;
long s1;
/*printf("\n请输入通讯录文件名:");
scanf("%s",filename); */

if((fp=fopen(filename,"at+"))==NULL)
{
printf("\n不能建立通讯录!");
exit(1);
}
fprintf(fp,"%-10s%-20s%-20%-50s\n","姓名","电话号码","qq号码","学校和专业");
printf("\n请输入姓名、电话号码、qq号码及学校专业(以0结束)\n");
scanf("%s",one.name);
while(strcmp(one.name,"0"))
{
scanf("%s%s",one.tel,one.qq,one.schpro);
fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.qq,one.schpro);
scanf("%s",one.name);
}
fclose(fp);
}


/*输出模块output( ):输出通讯录中联系人通讯信息的子函数*/
void output()
{
struct person one;
if((fp=fopen(filename,"r"))==NULL)
{
printf("\n不能打开通讯录!");
exit(1);
}
printf("\n\n%20s\n","通 讯 录");
while(!feof(fp))
{
fscanf(fp,"%s%s%s%s\n",one.name,one.tel,one.qq,one.schpro);
printf("%-10s%-20s%-20%-50s",one.name,one.tel,one.addr);
}
fclose(fp);
}


/*添加模块append( ):向通讯录中添加某人通讯信息的子函数*/
void append()
{
struct person one;
if((fp=fopen(filename,"a"))==NULL)
{
printf("\n不能打开通讯录!");
exit(1);
}
printf("\n请输入添加的姓名、电话号码、qq号码及学校专业\n");
scanf("%s%s%s",one.name,one.tel,one.qq,one.schpro);
fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.qq,one.schpro);
fclose(fp);
}


/*查找模块search( ):在通讯录中查找某人通讯信息的子函数*/
void search()
{
struct person one;   
int k,j=0;
if((fp=fopen(filename,"rb"))==NULL)
{
printf("\n不能打开通讯录!");
exit(1);
}
printf("\n姓名查找请按1");
printf("\n电话号码查找请按2");
scanf("%d",&k);
switch(k)
{
case 1:
{
char namekey[8];printf("\n请输入姓名: ");
scanf("%s",namekey);
while(!feof(fp))
{
fscanf(fp,"%s%s%s%s\n",one.name,one.tel,one.qq,one.schpro);
if(!strcmp(namekey,one.name))
{
printf("\n\n已查到,记录为:");
printf("\n%-10s%-20s%-20s0%-50s",one.name,one.tel,one.qq,one.schpro);
j=1;
}
}
if(!j)
printf("\n\n对不起,通讯录中没有此人的记录。");
fclose(fp);
} break;
case 2:
{char telkey[15];printf("\n请输入电话号码: ");
scanf("%s",telkey);
while(!feof(fp))
{
fscanf(fp,"%s%s%s%s\n",one.name,one.tel,one.qq,one.schpro);
if(!strcmp(telkey,one.tel))
{
printf("\n\n已查到,记录为:");
printf("\n%-10s%-20s%-20s0%-50s",one.name,one.tel,one.qq,one.schpro);
j=1;
}
}
if(!j)
printf("\n\n对不起,通讯簿中没有此人的记录。");
fclose(fp);
} break;
}
}

/*修改模块modify( ):在通讯录中修改某人通讯信息的子函数*/
void modify()
{   
int m,k=0;
long offset;
char namekey[8];
struct person one;
printf("\n请输入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{   
printf("\n不能打开通讯录!");
exit(1);
}
while(!feof(fp))
{   
offset=ftell(fp);
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.qq,one.schpro);
if(!strcmp(namekey,one.name))
{
k=1;
break;
}
}
if(k)
{   
printf("\n已查到,记录为:");
printf("\n%-10s%-18s%-50s",one.name,one.tel,one.qq,one.schpro);
printf("\n请输入新姓名、电话号码、qq号码及学校专业:");
scanf("%s%s%s",one.name,one.tel,one.qq,one.schpro);
fseek(fp,offset,SEEK_SET);
printf("%ld",ftell(fp));
fprintf(fp,"%-10s%-20s%-20s%-50s\n",one.name,one.tel,one.qq,one.schpro);
}
else
printf("\n对不起,通讯录中没有此人的记录。");
fclose(fp);
}


/*删除模块del( ):在通讯录中删除某人通讯信息的子函数*/
void del()
{   
int m,k = 0,flag;
long offset1,offset2;
char namekey[8], valid[4];
struct person one;
printf("\n请输入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{   
printf("\n不能打开通讯录!");
exit(1);
}
while(!feof(fp))
{   
offset1=ftell(fp);
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.qq,one.schpro);
if(!strcmp(namekey,one.name))
{
k=1;
break;
if(k)
{
printf("\n已查到,记录为");
printf("\n%-10s%-20s%-20s%-50s",one.name,one.tel,one.qq,one.schpro);
printf("\n确实要删除,按1;不删除,按0:");
scanf("%d",&m);
if(m)
{
fseek(fp,offset1,SEEK_SET);
fprintf(fp,"%-10s%-20s%-20%-50s\n","","","","");
}
else
printf("\n对不起,通讯录中没有此人的记录。");
fclose(fp);
}
}
}
}

回复列表 (共6个回复)

沙发

你的问题是什么,你要的效果是什么?

就这么贴一段程序,一点解释也没有,天书呢?

板凳

就是想做一个通讯录系统,可是总说modify和del两个函数定义错误特想把这个程序作出

3 楼


就是想做一个通讯录系统,可是总说modify和del两个函数定义错误特想把这个程序作出
帮帮我吧···

4 楼


就是想做一个通讯录系统,可是总说modify和del两个函数定义错误特想把这个程序作出
帮帮我吧

5 楼


[code=c]
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct person
{
    char name[8];
    char tel[15];
    char qq[12];
    char schpro[50];
    char addr[20];
};
char filename[23]="E:\\我的通讯录.txt";
FILE *fp;
void creat();
void output();
void search();
void append();
void modify();
void del();
void main(void)
{
    int m;
    creat();
    while(1)
    {
        printf("\n\t\t*********欢迎使用通讯录信息管理系统*********\n\n");
        printf("\n\t\t添加,请按1 查找,请按2");
        printf("\n\t\t修改,请按3 删除,请按4");
        printf("\n\t\t输出,请按5 退出,请按0");
        printf("\n\t\t\n");
        printf("\n\t\t********************************************\n\n");
        printf("请选择(0--5):");
        scanf("%d",&m);
        if(m>=0&&m<=5)
        {
            switch(m)
            {
            case 1:
                append();
                break;
            case 2:
                search();
                break;
            case 3:
                modify();
                break;
            case 4:
                del();
                break;
            case 5:
                output();
                break;
            case 0:
                exit(1);
            }
            printf("\n\n操作完毕,请再次选择!");
        }
        else
            printf("\n\n选择错误,请再次选择!");
    }
}

/*输入模块creat( ): 创建通讯录的子函数。*/
void creat()
{
    struct person one;
    long s1;
    /*printf("\n请输入通讯录文件名:");
    scanf("%s",filename); */

    if((fp=fopen(filename,"at+"))==NULL)
    {
        printf("\n不能建立通讯录!");
        exit(1);
    }
    char strName[15] = {0};
    fscanf(fp,"%s",strName);
    if (strcmp(strName,"姓名"))
    {
        fprintf(fp,"%-10s%-20s%-20s%-50s\n","姓名","电话号码","qq号码","学校和专业");
        printf("\n请输入姓名、电话号码、qq号码及学校专业(以0结束)\n");
        scanf("%s",one.name);
        while(strcmp(one.name,"0"))
        {
            scanf("%s%s%s",one.tel,one.qq,one.schpro);
            fprintf(fp,"%-10s%-20s%-20s%-50s\n",one.name,one.tel,one.qq,one.schpro);
            scanf("%s",one.name);
        }
    }
    fclose(fp);
}


/*输出模块output( ):输出通讯录中联系人通讯信息的子函数*/
void output()
{
    struct person one;
    if((fp=fopen(filename,"r"))==NULL)
    {
        printf("\n不能打开通讯录!");
        exit(1);
    }
    printf("\n\n%20s\n","通 讯 录");
    while(!feof(fp))
    {
        fscanf(fp,"%s%s%s%s\n",one.name,one.tel,one.qq,one.schpro);
        printf("%-10s%-20s%-20s%-50s",one.name,one.tel,one.qq,one.schpro);
    }
    fclose(fp);
}


/*添加模块append( ):向通讯录中添加某人通讯信息的子函数*/
void append()
{
    struct person one;
    if((fp=fopen(filename,"a"))==NULL)
    {
        printf("\n不能打开通讯录!");
        exit(1);
    }
    printf("\n请输入添加的姓名、电话号码、qq号码及学校专业\n");
    scanf("%s%s%s%s",one.name,one.tel,one.qq,one.schpro);
    fprintf(fp,"%-10s%-20s%-20s%-50s\n",one.name,one.tel,one.qq,one.schpro);
    fclose(fp);
}

[/code]

6 楼


[code=c]
/*查找模块search( ):在通讯录中查找某人通讯信息的子函数*/
void search()
{
    struct person one;
    int k,j=0;
    if((fp=fopen(filename,"rb"))==NULL)
    {
        printf("\n不能打开通讯录!");
        exit(1);
    }
    printf("\n姓名查找请按1");
    printf("\n电话号码查找请按2");
    scanf("%d",&k);
    switch(k)
    {
    case 1:
    {
        char namekey[8];
        printf("\n请输入姓名: ");
        scanf("%s",namekey);
        while(!feof(fp))
        {
            fscanf(fp,"%s%s%s%s\n",one.name,one.tel,one.qq,one.schpro);
            if(!strcmp(namekey,one.name))
            {
                printf("\n\n已查到,记录为:");
                printf("\n%-10s%-20s%-20s%-50s",one.name,one.tel,one.qq,one.schpro);
                j=1;
            }
        }
        if(!j)
            printf("\n\n对不起,通讯录中没有此人的记录。");
        fclose(fp);
    }
    break;
    case 2:
    {
        char telkey[15];
        printf("\n请输入电话号码: ");
        scanf("%s",telkey);
        while(!feof(fp))
        {
            fscanf(fp,"%s%s%s%s\n",one.name,one.tel,one.qq,one.schpro);
            if(!strcmp(telkey,one.tel))
            {
                printf("\n\n已查到,记录为:");
                printf("\n%-10s%-20s%-20s%-50s",one.name,one.tel,one.qq,one.schpro);
                j=1;
            }
        }
        if(!j)
            printf("\n\n对不起,通讯簿中没有此人的记录。");
        fclose(fp);
    }
    break;
    }
}

/*修改模块modify( ):在通讯录中修改某人通讯信息的子函数*/
void modify()
{
    int m,k=0;
    long offset;
    char namekey[8];
    struct person one;
    printf("\n请输入姓名:");
    scanf("%s",namekey);
    if((fp=fopen(filename,"r+"))==NULL)
    {
        printf("\n不能打开通讯录!");
        exit(1);
    }
    while(!feof(fp))
    {
        offset=ftell(fp);
        fscanf(fp,"%s%s%s%s\n",one.name,one.tel,one.qq,one.schpro);
        if(!strcmp(namekey,one.name))
        {
            k=1;
            break;
        }
    }
    if(k)
    {
        printf("\n已查到,记录为:");
        printf("\n%-10s%-20s%-20s%-50s",one.name,one.tel,one.qq,one.schpro);
        printf("\n请输入新姓名、电话号码、qq号码及学校专业:");
        scanf("%s%s%s%s",one.name,one.tel,one.qq,one.schpro);
        fseek(fp,offset,SEEK_SET);
        //printf("%ld",ftell(fp));
        fprintf(fp,"%-10s%-20s%-20s%-50s\n",one.name,one.tel,one.qq,one.schpro);
    }
    else
        printf("\n对不起,通讯录中没有此人的记录。");
    fclose(fp);
}


/*删除模块del( ):在通讯录中删除某人通讯信息的子函数*/
void del()
{
    int m,k = 0,flag;
    long offset1,offset2;
    char namekey[8], valid[4];
    struct person one;
    printf("\n请输入姓名:");
    scanf("%s",namekey);
    if((fp=fopen(filename,"r+"))==NULL)
    {
        printf("\n不能打开通讯录!");
        exit(1);
    }
    while(!feof(fp))
    {
        offset1=ftell(fp);
        fscanf(fp,"%s%s%s%s\n",one.name,one.tel,one.qq,one.schpro);
        if(!strcmp(namekey,one.name))
        {
            k=1;
            break;
        }
    }
    if(k)
    {
        printf("\n已查到,记录为");
        printf("\n%-10s%-20s%-20s%-50s",one.name,one.tel,one.qq,one.schpro);
        printf("\n确实要删除,按1;不删除,按0:");
        scanf("%d",&m);
        if(m)
        {
            fseek(fp,offset1,SEEK_SET);
            fprintf(fp,"%-10s%-20s%-20s%-50s","","","","");
        }
    }
    else
        printf("\n对不起,通讯录中没有此人的记录。");
    fclose(fp);

}


[/code]

我来回复

您尚未登录,请登录后再回复。点此登录或注册