标题:通讯录的源代码!
只看楼主
放荡不羁
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2008-1-18
 问题点数:0 回复次数:0 
通讯录的源代码!
发现论坛有人问C语言通讯录的问题,所以这里发个
以前写的小东西其实很简单!大家一定能看懂的~
在VC++60.里编译通过!
---------------------------------------------
#include <stdio.h>
#include <string.h>
struct person
{
char  name[18];
char  tel[15];
char  addr[50];
};
char filename[20];
FILE *fp;
void create();
void output();
void search();
void append();
void modify();
void delet();
main()
{
int m;
create();
while(1)
    {
    printf("\n\n添加,请按1");
    printf("\n查找,请按2");
    printf("\n修改,请按3");
    printf("\n删除,请按4");
    printf("\n输出,请按5");
    printf("\n退出,请按0\n");
    scanf("%d",&m);
    if(m>=0&&m<=5)
    {
        switch(m)
        {
        case 1 : append();
            break;
        case 2 : search();
            break;
        case 3 : modify();
            break;
        case 4 : delet();
            break;
        case 5 : output();
            break;
        //case 0 : exit();
        }
        printf("\n\n操作完毕,请再次选择!");
    }
    else
        printf("\n\n操作错误,请重新选择!");
    }   
}

void create()
{
struct person one;
//long s1;
printf("\n 请输入通讯簿名:");
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
  printf("\n不能建立通讯簿!");
  //exit();
}
fprintf(fp,"%-10s%-20s%-50s%\n","姓名","电话号码","住址");
printf("\n请输入姓名,电话号码及住址(以0结束)\n");
scanf("%s",one.name);
while(strcmp(one.name,"0"))
{
 scanf("%s%s",one.tel,one.addr);
fprintf(fp,"%-10s%-20s%-50s%\n",one.name,one.tel,one.addr);
scanf("%s",one.name);
}
fclose(fp);
}

void output()
{
struct person one;
if((fp=fopen(filename,"r"))==NULL)
{
  printf("\n不能打开通讯簿!");
  //exit();
}
printf("\n\n%20s\n","通 讯 簿");
while(!feof(fp))
    {
    fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
    printf("%-10s%-20s%-50s%\n",one.name,one.tel,one.addr);
    }    
fclose(fp);
}

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

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

void delet()
{
int m,k=0;
long offset1;
char namekey[8];
struct person one;
printf("\n请输入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
  printf("\n不能打开通讯簿!");
  //exit();
}
while(!feof(fp))
    {
    offset1=ftell(fp);
    fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
    if(!strcmp(namekey,one.name))
        {
        k=1;
        break;
        }
    }

if(k)
    {
    printf("\n已找到,记录为:");
    printf("%-10s%-20s%-50s%\n",one.name,one.tel,one.addr);
    printf("\n确实要删除,按1,不删除,按0:");
    scanf("%d",&m);
    if(m)
        {
        fseek(fp,offset1,SEEK_SET);
        fprintf(fp,"%-10s%-20s%-50s%\n","","","");
        }
    }

else
    printf("\n对不起!通讯簿中没有此人");
 fclose(fp);
}
搜索更多相关主题的帖子: 源代码 通讯录 void char 
2008-01-19 16:39



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-198030-1-1.html




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

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