qq_34883977 2016-05-04 12:32 采纳率: 0%
浏览 1407

为什么文件不能读取哇,其他的都可以,定义的学号是字符型~

#include "string.h"
#include "conio.h"
#include "stdlib.h"
#include "iostream"
using namespace std;
struct stde
{
char num[5];
char name[20];
float sco;
stde *next;
};
void mainmenu()
{
printf("======================\n");
printf("| 1.添加学生 |\n");
printf("| 2.查询学生 |\n");
printf("| 3.编辑学生 |\n");
printf("| 4.删除 |\n");
printf("| 5.打印信息 |\n");
printf("| 6.总人数 |\n");
printf("| 7.存盘 |\n");
printf("| 8.读取数据 |\n");
printf("| 9.退出程序 |\n");
printf("======================\n");
}

void add(struct stde *head)
{
system("cls");
static int n=1;
struct stde *p,*q;
char in;
p=(struct stde *)malloc(sizeof(struct stde));
do{
printf("添加学生信息\n");
printf("学号:");
scanf("%s",p->num);
if((p->num[3]>='0'&&p->num[3]<='9')&&p->num[4]=='\0')
{
break;
}
else
{
printf("请输入四位学号\n");
printf("任意键继续");
in=getch();
system("cls");
}
}while(1);

printf("姓名:");
scanf("%s",p->name);
printf("成绩:");
cin>>p->sco;
if(n==1)
{
    head->next=p;
    p->next=NULL;
    n++;        
}

else
{
    q=head;
    while(1)
    {
        if(q->next==NULL||(strcmp(p->num,q->num)>0&&strcmp(p->num,(q->next)->num)<0))
        {

            p->next=q->next;
            q->next=p;
            break;
        }
        q=q->next;
    }
}
system("cls");

}
void chaxun(struct stde *head)
{
system("cls");
char in;
struct stde *p;
int b=1;
char xh[5];do{
do{
printf("查询学生\n");
printf("学号:");
scanf("%s",xh);
if((xh[3]>='0'&&xh[3]<='9')&&xh[4]=='\0')
{
break;
}
else
{
printf("请输入四位学号\n");
printf("任意键继续");
in=getch();
system("cls");
}
}while(1);
p=head;

    while(1)
    {
        if(strcmp(p->num,xh)==0)
        {
            printf("姓名:");
            printf("%s\n",p->name);
            printf("成绩:");
            printf("%f\n",p->sco);
            b=0;
            break;
        }
        if(p->next==NULL)
        {
            break;
        }
        p=p->next;
    }
    if(b==0)
    {
        break;
    }
    else
    {
        printf("无该学生信息\n");
        break;
    }
}while(1);
printf("任意键继续");
in=getch();
system("cls");

}
void change(struct stde *head)
{
system("cls");
char in;
struct stde *p;
int b=1;
char xh[5];
if(head->next!=NULL){
do{
do{
printf("更改学生信息\n");
printf("学号:");
scanf("%s",xh);
if((xh[3]>='0'&&xh[3]<='9')&&xh[4]=='\0')
{
break;
}
else
{
printf("请输入四位学号\n");
printf("任意键继续");
in=getch();
system("cls");
}
}while(1);
p=head;

    while(1)
    {
        if(strcmp(p->num,xh)==0)
        {
            printf("姓名:");
            printf("%s\n",p->name);
            printf("成绩:");
            printf("%f\n",p->sco);
            b=0;
            break;
        }
        if(p->next==NULL)
        {
            break;
        }
        p=p->next;
    }
    if(b==0)
    {
        break;
    }
    else
    {
        printf("无该学生信息\n");
        printf("任意键继续");
        in=getch();
        system("cls");
    }
}while(1);
printf("请输入想要更改的内容\n");
printf("1.更改姓名\n");
printf("2.更改成绩\n");
in=getch();
if(in=='1')
{
    system("cls");
    printf("请输入更改的姓名:");
    scanf("%s",p->name);
}
if(in=='2')
{
    system("cls");
    printf("请输入更改的成绩:");
    cin>>p->sco;
}
}
else
{
    printf("无学生信息\n");
}
printf("任意键继续");
in=getch();
system("cls");

}
void print(struct stde *head)
{
system("cls");
struct stde *p;
char in;
p=head->next;
if(head->next!=NULL){
printf("打印学生信息\n");
while(1)
{
printf("学号");
printf("%s ",p->num);
printf("姓名");
printf("%s ",p->name);
printf("成绩");
printf("%f\n",p->sco);
if(p->next==NULL)
{
break;
}
p=p->next;
}
}
else
printf("无学生信息\n");
printf("任意键继续");
in=getch();
system("cls");
}
void all(struct stde *head)
{
system("cls");
char in;
struct stde *p;
p=head->next;
int i=0;
if(head->next==NULL)
i=0;
else{
while(1)
{
if(p->next!=NULL)
i++;
else
{
i++;
break;
}
p=p->next;
}
}
printf("总人数为:");
printf("%d\n",i);
printf("任意键继续");
in=getch();
system("cls");
}
void del(struct stde *head)
{
system("cls");
int b=1;
struct stde *p;
struct stde *q;
char in,xh[5];
if(head->next!=NULL){
p=head->next;
do{
do{
printf("删除学生\n");
while(1)
{

        printf("姓名:");
        printf("%s ",p->num);
        printf("学号:");
        printf("%s ",p->name);
        printf("成绩:");
        printf("%d\n",p->sco);
        if(p->next==NULL)
        {
            break;
        }
        p=p->next;
    }
    printf("请输入要删除学生学号:");
    scanf("%s",xh);
    if((xh[3]>='0'&&xh[3]<='9')&&xh[4]=='\0')
    {
        break;
    }
    else
    {
        printf("请输入四位学号\n");
        printf("任意键继续");
        p=head->next;
        in=getch();
        system("cls");
    }
}while(1);
p=head;

    while(1)
    {
        if(strcmp(p->num,xh)==0)
        {
            b=0;
            break;
        }
        if(p->next==NULL)
        {
            break;
        }
        p=p->next;
    }
    if(b==0)
    {
        break;
    }
    else
    {
        printf("无该学生信息\n");
        printf("任意键继续");
        p=head->next;
        in=getch();
        system("cls");
    }
}while(1);
q=head;
while(1)
{
    if(q->next==p)
    {
        break;
    }
    q=q->next;
}
q->next=p->next;
free(p);
printf("删除成功\n");
}
else
    printf("无学生信息\n");
printf("任意键继续");
in=getch();

}
struct stde *Insert(struct stde *head,struct stde *s)
{
struct stde *p1,*p2,*p0;
p1=(struct stde *)malloc(sizeof(struct stde));
p1=head;
p2=head;
p0=s;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{
while((strcmp(p1->num,s->num)>0)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(strcmp(p1->num,s->num)>0)
{
if(head==p1)
head=s;
else
p2->next=s;
s->next=p1;
}
else
{
p1->next=s;
s->next=NULL;
}
}
return(head);
}
void main()
{ FILE *fp;
stde *head;
head=(struct stde *)malloc(sizeof(struct stde));
strcpy(head->num,"0000");
head->next=NULL;
char in;
int bExit=0;
while(!bExit)
{
mainmenu();
in=getch();
switch(in)
{
case'1':add(head);break;
case'2':chaxun(head);break;
case'3':change(head);break;
case'4':del(head);system("cls");break;
case'5':print(head);break;
case'6':all(head);break;
case'7':system("cls");
struct stde *p;
p=head->next;
if((fp=fopen("D:\stduent.txt","a"))==NULL)
{
printf("can not open this file\n");
}
while(p!=NULL)
{
fprintf(fp,"%s %s %f\n",p->num,p->name,p->sco);
p=p->next;
}
fclose(fp);
printf("按任意键结束...");
getch();
system("cls");
break;
case '8':
if((fp=fopen("D:\stduent.txt","r"))==NULL)
{
printf("can not open this file\n");
}
struct stde *s;
while(1)
{
if(feof(fp)) break;
s=(struct stde *)malloc(sizeof(struct stde));
fscanf(fp,"%s%s%f",s->num,s->name,&(s->sco));
Insert(head,s);
}
fclose(fp);
printf("按任意键结束...");
getch();
system("cls");
break;
case'9':bExit=1;break;
default:system("cls");break;
}
}
}

  • 写回答

3条回答

  • devmiao 2016-05-04 23:28
    关注

    代码比较长,自己调试下

    评论

报告相同问题?

悬赏问题

  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿