| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 910 人关注过本帖
标题:请教一道题
只看楼主 加入收藏
thehero
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-7-8
收藏
 问题点数:0 回复次数:8 
请教一道题
题目: 从键盘上按“记录”方式输入一批学生数据,然后存到磁盘上。最后在键盘上按“记录”读出数据。


请各位高手不吝赐教,这是老师给我的一道课程设计题目。
搜索更多相关主题的帖子: 键盘 课程 数据 
2008-07-08 15:45
zmfttkl
Rank: 1
等 级:新手上路
帖 子:148
专家分:0
注 册:2007-7-1
收藏
得分:0 
顶一下!我也想知道撒!

2008-07-08 15:50
笨鸟学跑
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2008-7-8
收藏
得分:0 
还在看指针,所以不懂
2008-07-08 15:55
a383369542
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2008-7-8
收藏
得分:0 
程序我们注重思想,数据库我们注重原理,生活我们注重感情。07级程序&&数据库QQ交流群48335578,欢迎志士的加入
2008-07-08 16:02
小菌
Rank: 1
来 自:江苏扬州
等 级:新手上路
帖 子:9
专家分:0
注 册:2008-7-7
收藏
得分:0 
楼上的看过你好多次了
2008-07-08 18:48
独孤小梦
Rank: 1
等 级:新手上路
威 望:1
帖 子:338
专家分:0
注 册:2008-6-5
收藏
得分:0 
这个我来回答吧~!
可以使用FOR语句和fread函数读入学生数据:
for(i=0;i<40;i++)
fread(&stud[i],sizeof(struct student_type),1,fp);
FP为指向文件的文件指针!
输出时:
for(i=0;i<40;i++)
fwrite(&stud[i],sizeof(struct student_type),1,fp);
我不知道什么叫记录方式,我也是新手~

编程小菜
2008-07-08 18:55
独孤小梦
Rank: 1
等 级:新手上路
威 望:1
帖 子:338
专家分:0
注 册:2008-6-5
收藏
得分:0 
我不知道你说的"记录"方式是什么东西,我也是新手,下面的代码也是把学生数据存到磁盘上的!你看下~
#include"stdio.h"
#include"stdlib.h"
void main(int argc,char *argv[])
{
FILE *in,*out;
char ch;
if(agrc!=3)
{
printf("您忘记了输入一个文件名\n");
exit(0);
}
if((in=fopen(argv[1],"r"))==NULL)
{
printf("cannot open infile\n");
exit(0);
}
if((out=fopen(argv[2],"w"))==NULL)
{
printf("cannot open infile\n");
exit(0);
}
while(!feof(in))fputc(fgetc(in),out);
fclose(in);
flose(out);
}

编程小菜
2008-07-08 20:10
Rand
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2008-7-2
收藏
得分:0 
#include <stdio.h>
#include <malloc.h>
#define len sizeof(struct student)

#define len sizeof(struct student)
#define null 0
struct student
{char name[20];
 char num[8];
 int score;
struct student *next;};
void creat_txt()
{FILE *fp;
if((fp=fopen("f:\\student.txt","a"))==null)
{printf("open file error\n");exit(0);
}
fclose(fp);
}
void  inpu()
{
  struct student *p1,*p2,*head;
 char ch;int falg;
 void save(struct student *head);
 creat_txt();
 p1=p2=(struct student *)malloc(len);
 printf("name=");scanf("%s",&p1->name);
 printf("number=");scanf("%s",&p1->num);
 printf("score=");scanf("%d",&p1->score);
   head=p1;head->next=null;
  while(1)
  {printf("press 0 to end,any key to continue...\n");
  getchar();
  if(ch=getchar()=='0')break;
  printf("input student name num score:\n");
  p1=(struct student *)malloc(len);
  printf("name=");scanf("%s",&p1->name);
  printf("number=");scanf("%s",&p1->num);
  printf("score=");scanf("%d",&p1->score);
  
   
  p2->next=p1;
  p1->next=null;
  p2=p1;
  }
  printf("save,please wait......\n");
  save(head);
  
}

void list()
{FILE *list;
struct student temp;
struct student out;
temp.name[0]='0';
if((list=fopen("f:\\student.txt","r"))==null)
{printf("open file error \n");exit(0);
}
printf("name       number       score\n");
while(!feof(list))
{
if(temp.name!=0);
{out.name=temp.name;
out.num=temp.num;
out.score=temp.score;}
fread(&temp,len,1,list);
if(out.name[0]!='0')printf("%-10s %s %5d\n",out.name,out.num,out.score);
}
printf("\n");
fclose(list);

}
void save(struct student *head)
{FILE *open;
struct student *p1;
p1=head;
if((open=fopen("f:\\student.txt","a"))==null)
{printf("open file error\n");exit(0);
}
while(p1)
{fwrite(p1,len,1,open);
 //fprintf(open,"\n");
p1=p1->next;
printf("1 successful.....\n");
}
fclose(open);
printf("save successful\n");
getchar();
}

struct student *look_for(struct student *head,struct student key)
{struct student *p1;
 p1=head;
 while(1)
 {if(p1->next==null){p1=null;break;}
 if(strcmp(p1->name,key.name)==0)break;
 else
 p1=p1->next;
 }
 
 return(p1);
}


void look()
{struct student *read_in_link();
void list();
struct student *look_for(struct student *head,struct student key);
struct student key;
struct student *answer;
struct student *head;
list();
printf("please input name.\n");

scanf("%s",&key.name);
getchar();
head=read_in_link();
answer=look_for(head,key);
if(answer!=null)
printf("%s,   %s,   %d\n",answer->name,answer->num,answer->score);
else
printf("this person is not in!\n");

}

struct student *read_in_link()
{FILE *fp;
 struct student *p1,*p2,*head;
 struct student stu;
void print_link(struct student *head);


if((fp=fopen("f:\\student.txt","r+"))==null)
{printf("open file error \n");exit(0);
}
p1=p2=(struct student *)malloc(len);
head=p1;
while(!feof(fp))
{fread(&stu,len,1,fp);
p1->name=stu.name;p1->num=stu.num;
p1->score=stu.score;
p2->next=p1;
p2=p1;
p1->next=null;
p1=(struct student *)malloc(len);
}
p2->next=null;
free(p1);
fclose(fp);
//printf("name=%s  num=%s   score=%d \n",head->name,head->num,head->score);
//print_link(head);
return(head);
}


void print_link(struct student *head)
{struct student *p1;
 p1=head;
 printf("list of your database\n");
 while(p1->next!=null)
 {printf("%s  %s  %d\n",p1->name,p1->num,p1->score);
 p1=p1->next;}
 printf("===========================\n");
}


void delet()
{
void del(struct student *head,struct student *answer);
struct student *read_in_link();
void list();
struct student *look_for(struct student *head,struct student key);
struct student key,*head,*answer;
printf("please input the person'name that you want't to del\n");
scanf("%s",key.name);
head=read_in_link();
answer=look_for(head,key);
//printf("%s   %s  %d",answer->name,answer->num,answer->score);
if(answer!=null)del(head,answer);

else printf("this person is not in!\n");
}
void del(struct student *head,struct student *answer)
{struct student *p1;
 struct student *p2;
 void rewrite(struct student *head);

p1=p2=head;

if(answer==head)
{head=head->next;
rewrite(head);
}
else
{
while(strcmp(p1->name,answer->name)==0);
{
p2=p1;
p1=p1->next;
}
p2->next=p1->next;
free(p1);
print_link(head);
rewrite(head);
list();
}
}
void rewrite(struct student *head)
{FILE *open;
void print_link(struct student *head);
struct student *p1;
p1=head;
if((open=fopen("f:\\student.txt","w+"))==null)
{printf("open file error\n");exit(0);
}
while(p1->next!=null)
{fwrite(p1,len,1,open);
p1=p1->next;
printf("1 successful.....\n");
}
fclose(open);
printf("del successful\n");
print_link(head);
getchar();
}


void print()
{printf("         =======================================\n");
 printf("           1. input student messige.\n");
 printf("           2.look for student messige.\n");
 printf("           3.delet student message.\n");
 printf("           4.list the database:\n");
 printf("           5.exit\n");
 printf("         ======================================\n");
}


void menu()
{int key;
void inpu(void);
void look(void);
void delet(void);
while(1)
{print();
scanf("%d",&key);
if(key<0||key>9)printf("error\n");
switch(key)
{case 1:
 inpu();break;
 case 2:
  look();break;
  case 3:
  delet();break;
  case 4:
  list();break;
  case 5:
  exit(1);break;
  
}
}
}
main()
{
menu();
}
以前写的,你把不要的功能删掉吧.
代码没优化过,也没做过什么注释,你将就参考参考吧~
2008-07-08 20:38
thehero
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-7-8
收藏
得分:0 
谢谢各位 高手
2008-07-09 18:07
快速回复:请教一道题
数据加载中...
 
   



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

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