| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2263 人关注过本帖
标题:怎么把输入的通讯录导入到txt
只看楼主 加入收藏
lihaoyu321
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2016-7-7
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
怎么把输入的通讯录导入到txt
程序代码:
#include"stdio.h" 
#include"stdlib.h" 
#include"string.h" 
struct student 
{ 
int no; /*学号*/ 
char name[20]; /*姓名*/ 
char acdamic[20]; /*院系*/ 
char major[20]; /*专业*/ 
char province[20]; /*籍贯*/ 
char address[40]; /*家庭地址*/ 
long phone; /*联系电话*/ 
struct student *next; 
}; 
struct student *input(); 
void print(struct student *h); 
struct student *insert(struct student *h); 
struct student *del(struct student *h); 
void find4(struct student *h); 
void find3(struct student *h); 
void find2(struct student *h); 
void find1(struct student *h); 
struct student *head=NULL; 
char ch,*menu[]={"------------------通讯录菜单---------------", 
"1.----------建立学生通讯录-----------------", 
"2.----------输出全部学生通讯录-------------", 
"3.----------增加的学生的个数---------------", 
"4.----------删除指定学号的学生-------------", 
"5.----------按系别查找学生信息-------------", 
"6.----------按专业查找学生信息-------------", 
"7.----------按姓名查找学生信息-------------", 
"8.----------按学号查找学生信息-------------", 
"9.----------退出通讯录---------------------"}; 
struct student *input() /*输入函数*/ 
{ 
int n; 
printf("请输入你要建立的学生通讯录的学生个数:\n"); 
scanf("%d",&n); 
struct student *h=NULL,*p,*q; 
int i; 
for(i=1;i<=n;i++) 
{ 
printf("请依次输入第%d个学生的学号,姓名,系别,专业,籍贯,家庭住址和联系电话:\n",i); 
p=(struct student *)malloc(sizeof(struct student)); 
if(p==NULL) 
{ 
printf("内存不足!\n"); 
exit(0); 
} 
scanf("%d%s%s%s%s%s%ld",&p->no,p->name,p->acdamic,p->major,p->province,p->address,&p->phone); 
if(i==1)h=p; 
else q->next=p; 
q=p; 
} 
q->next=NULL; 
return h; 
} 
void print(struct student *h) /*输出函数*/ 
{ 
struct student *p=h; 
while(p) 
{ 
printf("学生信息:\n%d,%s,%s,%s,%s,%s,%ld\n",p->no,p->name,p->acdamic,p->major,p->province,p->address,p->phone); 
p=p->next; 
} 
} 
struct student *insert(struct student *h) /*增加学生信息*/ 
{ 
int n; 
printf("请输入你要增加的学生的个数:\n"); 
scanf("%d",&n); 
struct student *p,*r; 
int i; 
for(i=1;i<=n;i++) 
{ 
r=(struct student *)malloc(sizeof(struct student)); 
printf("请输入第%d个你要插入的学生的信息:\n",i); 
scanf("%d%s%s%s%s%s%ld",&r->no,r->name,r->acdamic,r->major,r->province,r->address,&r->phone); 
p=h; 
h=r; 
r->next=p; 
} 
return h; 
} 
struct student *del(struct student *h) /*按学号进行删除*/ 
{ 
int n; 
printf("请输入要删除的学生的学号:\n"); 
scanf("%d",&n); 
struct student *p,*q; 
if(h=NULL)printf("empty list!\n"); 
else 
{ 
p=h; 
while(p->no!=n&&p->next) 
{q=p;p=p->next;} 
if(p->no==n) 
{ 
if(h==p)h=p->next; 
else q->next=p->next; 
} 
else printf("%d is not found!\n"); 
} 
return h; 
} 
void find1(struct student *h) /*按院系查找学生信息*/ 
{ 
char s[40]; 
printf("请输入要查找的学生的院系:\n"); 
scanf("%s",s); 
struct student *p; 
if(h==NULL)printf("empty list!\n"); 
else 
{ 
p=h; 
while(p) 
{ 
if(strcmp(p->acdamic,s)==0) 
printf("学生信息:\n%d %s %s %s %s %s %ld\n",p->no,p->name,p->acdamic,p->major,p->province,p->address,p->phone); 
p=p->next; 
} 
} 
} 
void find2(struct student *h) /*按专业查找*/ 
{ 
char s[20]; 
printf("请输入要查找的学生的专业名:\n"); 
scanf("%s",s); 
struct student *p; 
if(h==NULL)printf("empty list!\n"); 
else 
{ 
p=h; 
while(p) 
{ 
if(strcmp(p->major,s)==0) 
printf("学生信息:\n%d %s %s %s %s %s %ld\n",p->no,p->name,p->acdamic,p->major,p->province,p->address,p->phone); 
p=p->next; 
} 
} 
} 
void find3(struct student *h) /*按姓名查找*/ 
{ char ss[10]; 
printf("请输入要查找的学生的姓名:\n"); 
scanf("%s",ss); 
struct student *p; 
if(h==NULL)printf("empty list!\n"); 
else 
{ 
p=h; 
while(p) 
{ 
if(strcmp(p->name,ss)==0) 
printf("学生信息:\n%d %s %s %s %s %s %ld\n",p->no,p->name,p->acdamic,p->major,p->province,p->address,p->phone); 
p=p->next; 
} 
} 
} 
void find4(struct student *h) /*按学号查找*/ 
{ 
int num; 
printf("请输入要查找的学生的学号:\n"); 
scanf("%d",&num); 
struct student *p; 
if(h==NULL)printf("empty list!\n"); 
else 
{ 
p=h; 
while(p) 
{ 
if(p->no==num) 
printf("学生信息:\n%d %s %s %s %s %s %ld\n",p->no,p->name,p->acdamic,p->major,p->province,p->address,p->phone); 
else 
printf("没有你要查找的学生信息!\n"); 
p=p->next; 
} 
} 
} 
int menu_select() 
{ 
int i,s; 
char c[3]; 
for(i=0;i<10;i++) 
printf("%s\n",menu[i]); 
do 
{ 
scanf("%s",c); 
s=atoi(c); 
}while(s<0||s>9); 
return s; 
} 
main() 
{ 
for(;;) 
{ 
switch(menu_select()) 
{ 
case 1:head=input();break; 
case 2:print(head);break; 
case 3:head=insert(head);break; 
case 4:head=del(head);break; 
case 5:find1(head);break; 
case 6:find2(head);break; 
case 7:find3(head);break; 
case 8:find4(head);break; 
case 9:exit(0); 
} 
} 
}


求教
搜索更多相关主题的帖子: 通讯录 
2016-07-07 16:44
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:10 
增加了一个选项,你可以自行调整

程序代码:
#include"stdio.h" 
#include"stdlib.h" 
#include"string.h" 
struct student
{
    int no; /*学号*/
    char name[20]; /*姓名*/
    char acdamic[20]; /*院系*/
    char major[20]; /*专业*/
    char province[20]; /*籍贯*/
    char address[40]; /*家庭地址*/
    long phone; /*联系电话*/
    struct student *next;
};
struct student *input();
void print(struct student *h);
struct student *insert(struct student *h);
struct student *del(struct student *h);
void find4(struct student *h);
void find3(struct student *h);
void find2(struct student *h);
void find1(struct student *h);
struct student *head = NULL;
char ch, *menu[] = { "------------------通讯录菜单---------------",
"1.----------建立学生通讯录-----------------",
"2.----------输出全部学生通讯录-------------",
"3.----------增加的学生的个数---------------",
"4.----------删除指定学号的学生-------------",
"5.----------按系别查找学生信息-------------",
"6.----------按专业查找学生信息-------------",
"7.----------按姓名查找学生信息-------------",
"8.----------按学号查找学生信息-------------",
"0.----------导出txt信息--------------------",
"9.----------退出通讯录---------------------" };
struct student *input() /*输入函数*/
{
    int n;
    printf("请输入你要建立的学生通讯录的学生个数:\n");
    scanf("%d", &n);
    struct student *h = NULL, *p, *q = h;
    int i;
    for (i = 1; i <= n; i++)
    {
        printf("请依次输入第%d个学生的学号,姓名,系别,专业,籍贯,家庭住址和联系电话:\n", i);
        p = (struct student *)malloc(sizeof(struct student));
        if (p == NULL)
        {
            printf("内存不足!\n");
            exit(0);
        }
        scanf("%d%s%s%s%s%s%ld", &p->no, p->name, p->acdamic, p->major, p->province, p->address, &p->phone);
        if (i == 1)h = p;
        else q->next = p;
        q = p;
    }
    q->next = NULL;
    return h;
}
void print(struct student *h) /*输出函数*/
{
    struct student *p = h;
    while (p)
    {
        printf("学生信息:\n%d,%s,%s,%s,%s,%s,%ld\n", p->no, p->name, p->acdamic, p->major, p->province, p->address, p->phone);
        p = p->next;
    }
}
struct student *insert(struct student *h) /*增加学生信息*/
{
    int n;
    printf("请输入你要增加的学生的个数:\n");
    scanf("%d", &n);
    struct student *p, *r;
    int i;
    for (i = 1; i <= n; i++)
    {
        r = (struct student *)malloc(sizeof(struct student));
        printf("请输入第%d个你要插入的学生的信息:\n", i);
        scanf("%d%s%s%s%s%s%ld", &r->no, r->name, r->acdamic, r->major, r->province, r->address, &r->phone);
        p = h;
        h = r;
        r->next = p;
    }
    return h;
}
struct student *del(struct student *h) /*按学号进行删除*/
{
    int n;
    printf("请输入要删除的学生的学号:\n");
    scanf("%d", &n);
    struct student *p, *q = h;
    if (h = NULL)printf("empty list!\n");
    else
    {
        p = h;
        while (p->no != n&&p->next)
        {
            q = p; p = p->next;
        }
        if (p->no == n)
        {
            if (h == p)h = p->next;
            else q->next = p->next;
        }
        else printf("%d is not found!\n",n);
    }
    return h;
}
void find1(struct student *h) /*按院系查找学生信息*/
{
    char s[40];
    printf("请输入要查找的学生的院系:\n");
    scanf("%s", s);
    struct student *p;
    if (h == NULL)printf("empty list!\n");
    else
    {
        p = h;
        while (p)
        {
            if (strcmp(p->acdamic, s) == 0)
                printf("学生信息:\n%d %s %s %s %s %s %ld\n", p->no, p->name, p->acdamic, p->major, p->province, p->address, p->phone);
            p = p->next;
        }
    }
}
void find2(struct student *h) /*按专业查找*/
{
    char s[20];
    printf("请输入要查找的学生的专业名:\n");
    scanf("%s", s);
    struct student *p;
    if (h == NULL)printf("empty list!\n");
    else
    {
        p = h;
        while (p)
        {
            if (strcmp(p->major, s) == 0)
                printf("学生信息:\n%d %s %s %s %s %s %ld\n", p->no, p->name, p->acdamic, p->major, p->province, p->address, p->phone);
            p = p->next;
        }
    }
}
void find3(struct student *h) /*按姓名查找*/
{
    char ss[10];
    printf("请输入要查找的学生的姓名:\n");
    scanf("%s", ss);
    struct student *p;
    if (h == NULL)printf("empty list!\n");
    else
    {
        p = h;
        while (p)
        {
            if (strcmp(p->name, ss) == 0)
                printf("学生信息:\n%d %s %s %s %s %s %ld\n", p->no, p->name, p->acdamic, p->major, p->province, p->address, p->phone);
            p = p->next;
        }
    }
}
void find4(struct student *h) /*按学号查找*/
{
    int num;
    printf("请输入要查找的学生的学号:\n");
    scanf("%d", &num);
    struct student *p;
    if (h == NULL)printf("empty list!\n");
    else
    {
        p = h;
        while (p)
        {
            if (p->no == num)
                printf("学生信息:\n%d %s %s %s %s %s %ld\n", p->no, p->name, p->acdamic, p->major, p->province, p->address, p->phone);
            else
                printf("没有你要查找的学生信息!\n");
            p = p->next;
        }
    }
}
void export_info(struct student *h) /*按学号查找*/
{
    int num;
    FILE *fp = fopen("d:\\record.txt", "w+");;
    struct student *p;
    if (h != NULL)
    {
        p = h;
        while (p)
        {
            fprintf(fp,"学生信息:\n%d %s %s %s %s %s %ld\n", p->no, p->name, p->acdamic, p->major, p->province, p->address, p->phone);
            p = p->next;
        }
    }
    fclose(fp);
}
int menu_select()
{
    int i, s;
    char c[3];
    for (i = 0; i<11; i++)
        printf("%s\n", menu[i]);
    do
    {
        scanf("%s", c);
        s = atoi(c);
    } while (s<0 || s>9);
    return s;
}
main()
{
    for (;;)
    {
        switch (menu_select())
        {
        case 1:head = input(); break;
        case 2:print(head); break;
        case 3:head = insert(head); break;
        case 4:head = del(head); break;
        case 5:find1(head); break;
        case 6:find2(head); break;
        case 7:find3(head); break;
        case 8:find4(head); break;
        case 0:export_info(head); break;
        case 9:exit(0);
        }
    }
}
2016-07-07 17:03
linlulu001
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:20
帖 子:944
专家分:4047
注 册:2016-4-13
收藏
得分:10 
不会吧,文件的读取,存入完全不会??
FILE *fp;  //文件指针
fp=NULL;
int i=5;
char a[]="abcdefg";
fp=fopen("addrlist.txt","w+");  //这是默认路径,如果不用用默认路径可以改成fp=fopen("d:\\addrlist.txt","w+");
if(fp==NULL) exit(0);
else fprintf(文件指针,格式字符串,输出表列);//将数据存入txt中,fprintf(fp,"%d,%s",i,a);类似printf的用法,只不过是多了个文件指针。
fclose(fp);   //记住用完文件之后要关闭它。
大概写法就是这样。

[此贴子已经被作者于2016-7-7 17:14编辑过]

2016-07-07 17:12
lihaoyu321
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2016-7-7
收藏
得分:0 
回复 3楼 linlulu001
额 版主大人 能不能带到我的程序里啊 自己调不对
2016-07-07 17:21
linlulu001
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:20
帖 子:944
专家分:4047
注 册:2016-4-13
收藏
得分:0 
刚刚复制2楼的代码准备写的时候看到他已经做好了,你自己仔细找下。
顺便说一下,你的代码看着真累,2楼的就舒服多了。
2016-07-07 17:33
lihaoyu321
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2016-7-7
收藏
得分:0 
回复 2楼 grmmylbs
这个导出的txt在哪呢

[此贴子已经被作者于2016-7-7 19:13编辑过]

2016-07-07 19:11
快速回复:怎么把输入的通讯录导入到txt
数据加载中...
 
   



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

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