| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 603 人关注过本帖
标题:小程序,希望各位高手指点
只看楼主 加入收藏
XevierCheng
Rank: 2
来 自:江西省景德镇市
等 级:论坛游民
帖 子:2
专家分:10
注 册:2010-10-8
结帖率:0
收藏
已结贴  问题点数:5 回复次数:3 
小程序,希望各位高手指点
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct pnode
{
    char name[8];
    char tel[16];
    char addr[30];
}personnode;

char filename[20];
FILE *fp;

void creat()
{
    personnode *person;
    person=(personnode *)malloc(sizeof(personnode));
    printf("\n请输入通讯录名:");
    scanf("%s",filename);
    if((fp=fopen(filename,"w"))==NULL)
    {
        printf("\n没有输入通讯录名,不能建立通讯录!");
        exit(0);
    }
    fprintf(fp,"%-10s%-20s%-50s\n","姓名","电话号码","住址");
    printf("\n请输入姓名,电话号码及住址(以空格隔开,以@结束)\n");
    scanf("%s",person->name);
    while(strcmp(person->name,"@"))
    {
        scanf("%s%s",person->tel,person->addr);
        fprintf(fp,"%-10s%-20s%-50s\n",person->name,person->tel,person->addr);
        scanf("%s",person->name);
    }
    fclose(fp);
}

void output()
{
    personnode *person;
    if((fp=fopen(filename,"r"))==NULL)
    {
        printf("\n不能打开通讯录!");
        exit(0);
    }
    printf("\n*********************************************\n");
    printf("%24s\n","通讯录");
    while(!feof(fp))
    {
        fscanf(fp,"%s%s%s\n",person->name,person->tel,person->addr);
        printf("%-10s%-20s%-50s\n",person->name,person->tel,person->addr);
    }
    fclose(fp);
    printf("*********************************************\n\n");
}

void append()
{
    personnode *person;
    person=(personnode *)malloc(sizeof(personnode));
    if((fp=fopen(filename,"a"))==NULL)
    {
        printf("\n不能打开通讯录!");
        exit(0);
    }
    printf("\n请输入添加的姓名,电话号码及住址\n");
    scanf("%s%s%s",person->name,person->tel,person->addr);
    fprintf(fp,"%-10s%-20s%-50s\n",person->name,person->tel,person->addr);
    fclose(fp);
}

void search()
{
    int k=0;
    char namekey[8];
    personnode *person;
    printf("\n请输入需要修改记录的姓名:");
    scanf("%s",namekey);
    if((fp=fopen(filename,"rb"))==NULL)
    {
        printf("\n不能打开通讯录!");
        exit(0);
    }
    while(!feof(fp))
    {
        fscanf(fp,"%s%s%s\n",person->name,person->tel,person->addr);
        if(!strcmp(namekey,person->name))
        {
            printf("\n\n已查到,记录为:");
            printf("%-10s%-20s%-50s\n",person->name,person->tel,person->addr);
            k=1;
        }
    }
    if(!k) printf("\n\n对不起,通讯录中没有此人的记录。\n");
    fclose(fp);
}

void modify()
{
    int k=0;
    long offset;
    char namekey[8];
    personnode *person;
    printf("\n请输入需要修改记录的姓名:");
    scanf("%s",namekey);
    if((fp=fopen(filename,"r+"))==NULL)
    {
        printf("\n不能打开通讯录!");
        exit(0);
    }
    while(!feof(fp))
    {
        offset=ftell(fp);
        fscanf(fp,"%s%s%s\n",person->name,person->tel,person->addr);
        if(!strcmp(namekey,person->name))
        {
            k=1;
            break;
        }
    }
    if(k)
    {
            printf("\n\n已查到,记录为:");
            printf("%-10s%-20s%-50s\n",person->name,person->tel,person->addr);
            printf("\n请输入新姓名,电话号码及住址:");
            scanf("%s%s%s",person->name,person->tel,person->addr);
            fseek(fp,offset,SEEK_SET);
            printf("%ld",ftell(fp));
            fprintf(fp,"%-10s%-20s%-50s\n",person->name,person->tel,person->addr);
    }
    else printf("\n对不起,通讯录中没有此人的记录。\n");
    fclose(fp);
}

void delet()
{
    int k=0;
    char m;
    long offset1;
    char namekey[8];
    personnode *person;
    printf("\n请输入删除记录的姓名:");
    scanf("%s",namekey);
    if((fp=fopen(filename,"r+"))==NULL)
    {
        printf("\n不能打开通讯录!");
        exit(0);
    }
    while(!feof(fp))
    {
        offset1=ftell(fp);
        fscanf(fp,"%s%s%s\n",person->name,person->tel,person->addr);
        if(!strcmp(namekey,person->name))
        {
            k=1;
            break;
        }
    }
    if(k)
    {
            printf("\n\n已查到,记录为:");
            printf("%-10s%-20s%-50s\n",person->name,person->tel,person->addr);
            printf("\n确实要删除?y/n");
            scanf("%c",&m);
            if(m='y')
            {
                fseek(fp,offset1,SEEK_SET);
                fprintf(fp,"%-14s%-24s%-48s\n"," "," "," ");
            }
    }
    else printf("\n对不起,通讯录中没有此人的记录。\n");
    fclose(fp);
}

void main()
{
    int m,flag=1;
    creat();
    while(flag)
    {
        printf("-----------------------------------------\n");
        printf("\t\t1-------添加\n");
        printf("\t\t2-------查找\n");
        printf("\t\t3-------修改\n");
        printf("\t\t4-------删除\n");
        printf("\t\t5-------输出\n");
        printf("\t\t6-------退出\n");
        printf("-----------------------------------------\n");
        printf("\t请选择(1~~~6)\n");
        scanf("%d",&m);
        switch(m)
        {
        case 1: append(); break;
        case 2: search(); break;
        case 3: modify(); break;
        case 4: delet(); break;
        case 5: output(); break;
        case 6: exit(0);
        }
    }
}
后面的修改、查找、删除、输出都运行不了,C++停止工作怎么回事?求教


搜索更多相关主题的帖子: 通讯录 电话 姓名 
2010-12-27 22:33
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:2 
personnode *person = (personnode *)malloc(sizeof(personnode));
2010-12-28 18:15
xy2bl
Rank: 1
等 级:新手上路
帖 子:61
专家分:2
注 册:2010-12-2
收藏
得分:2 
路过
2010-12-31 01:33
aaronhexin
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:67
专家分:199
注 册:2010-12-6
收藏
得分:2 
2楼正解,你很多函数里没有给personnode *person分配存储空间,这样你从文件读记录fscanf是不成功的。
2011-01-03 11:54
快速回复:小程序,希望各位高手指点
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026706 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved