高手进来看一下好吧,我真的不知道那里错了。。
#include "stdafx.h"#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define N 2
struct student
{
char num[10];
char name[10];
char tel[10];
};
void myprint();
void mycreat(struct student *p,int n);
void mydisplay(struct student *p,int n);
void mysearch(struct student *p,int n);
void mymodify(struct student *p,int n);
void myadd();
void mydelete(){};
//
void myadd(struct student *p,int m)
{
int n; int i;FILE *fp;
printf("请输入要增加的员工数目:");
scanf("%d",&n);
system("cls");
for(i=0;i<n;i++)
{
fp=fopen("jilu.txt","w+");
if(fp==NULL){printf("\n Error \n");return;}
printf("\n");
printf("请输入记录:\n");
printf("学号");
do
{
scanf("%s",p->num);
}while(strcmp(p->num,"")==0);
printf("姓名");scanf("%s",p->name);
printf("电话");scanf("%s",p->tel);
fprintf(fp,"%23s%15s%15s\n",p->num,p->name,p->tel);
}
}
void mysearch(struct student *p,int n)
{
int i=1;FILE *fp;char num[10];
fp=fopen("jilu.txt","r");
if(fp==NULL){printf("\n Error \n");return;}
system("cls");
printf("请输入要查找的编号:");
gets(num);
while(n>0)
{
fscanf(fp,"%23s%15s%15s\n",p->num,p->name,p->tel);
if(strcmp(p->num,num)==0)
{
printf("%23s%15s%15s\n",p->num,p->name,p->tel);
break;
}
p++;n--;
}
if(n==0)
printf("error");
}
void main()
{
char choose ,yes_no;
int M=N;
struct student record[N];
do
{
myprint();
printf(" ");
choose=getche();
switch(choose)
{
case '1':mycreat(record,N);break;
case '2':mydisplay(record,M);break;
case '3':mysearch(record,M);break;
case '4':mymodify(record,M);break;
case '5':myadd(record,M);M++;break;
case '6':mydelete();break;
case '0':break;
default:printf("\n %cfeifa\n",choose);
}
if(choose=='0') break;
printf("\n 要继续选择吗(y/n)?\n");
do
{
yes_no=getch();
}while(yes_no!='Y'&&yes_no!='y'&&yes_no!='n'&&yes_no!='N');
}while(yes_no=='Y'||yes_no=='y');
}
void myprint()
{
system("cls");
printf("请输入选项(0~6)\n");
printf("1--创建员工信息系统\n");
printf("2--显示员工信息\n");
printf("3--查询记录\n");
printf("4--修改员工信息\n");
printf("5--添加员工信息\n");
printf("6--删除员工信息\n");
printf("0--退出\n");
}
void mycreat(struct student *p,int n)
{
int i=1;FILE *fp;
fp=fopen("jilu.txt","w");
if(fp==NULL){printf("\n Error \n");return;}
system("cls");
while(i<=n)
{
printf("\n");
printf("请输入第%d个记录:\n",i);
printf("学号");
do
{
scanf("%s",p->num);
}while(strcmp(p->num,"")==0);
printf("name");scanf("%s",p->name);
printf("phone");scanf("%s",p->tel);
fprintf(fp,"%23s%15s%15s\n",p->num,p->name,p->tel);
p++;i++;
}
fclose(fp);
}
void mydisplay(struct student *p,int n)
{
FILE *fp;
fp=fopen("jilu.txt","r");
i
panda 22:56:51
f(fp==NULL){printf("\n Error \n");return;}
system("cls");
printf(" 学号 姓名 电话号码\n");
while(n>0)
{
fscanf(fp,"%23s%15s%15s\n",p->num,p->name,p->tel);
printf("%23s%15s%15s\n",p->num,p->name,p->tel);
p++;n--;
}
fclose(fp);
}
void mymodify(struct student *p,int n)
{
int i;FILE *fp;
char nam[10]; struct student *q=p;
mydisplay(p,n);
printf("请输入要修改的记录姓名:");
do
{
gets(nam);
}while(strcmp(nam,"")==0);
for(i=0;i<n;i++,p++)
{
if(strcmp(nam,p->name)==0)
break;
}
if(i==n)
printf("nobody");
else
{
printf("请输入正确的学号:");
do
{
gets(p->num);
}while(strcmp(p->num,"")==0);
printf("请输入正确姓名:");gets(p->name);
printf("请输入正确电话号码:");gets(p->tel);
fp=fopen("jilu.txt","w");
if(fp==NULL){printf("\n Error \n");return;}
for(i=0;i<n;i++,q++)
fprintf(fp,"%23s%15s%15s\n",p->num,p->name,p->tel);
}
fclose(fp);
}
可是add()函数实现加入的员工信息在显示出来的时候是乱码
估计是输入输出流的错误