标题:求教文件屯的问题
只看楼主
leodjh
Rank: 1
等 级:新手上路
帖 子:15
专家分:7
注 册:2016-8-13
结帖率:75%
已结贴  问题点数:10 回复次数:7 
求教文件屯的问题
我运行程序将一些信息保存在date.dat的文件中,用exe程序读取文件信息可以正常显示,但是直接打开data.dat文件显示的却是一堆的屯,向大神求教为什么?
搜索更多相关主题的帖子: 运行程序 信息 
2016-10-09 16:28
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:5 
是可显示字符吗,如果不是,那还要说什么
2016-10-09 16:31
leodjh
Rank: 1
等 级:新手上路
帖 子:15
专家分:7
注 册:2016-8-13
得分:0 
大神,都是可显示的,写入的是学生管理系统的学生信息
2016-10-09 16:40
leodjh
Rank: 1
等 级:新手上路
帖 子:15
专家分:7
注 册:2016-8-13
得分:0 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void display();
struct score
{
    int math_score;
    int English_score;
    int c_score;
};
struct student
{
        long sum;
        char name[30];
        char sex[10];
        char birth[20];
        struct score score1;
        struct student *next;
};
typedef struct student stu;
void meum(void)
{
    printf("1输入学生信息\n");
    printf("2查询学生信息\n");
    printf("3排序\n");
    printf("4删除信息\n");
    printf("5退出\n");
}
struct student* cz1()
{
    stu *p1,*p2,*head;
    int n=0,m;
    p1=p2=(struct student*)malloc(sizeof(struct student));

    if(p1!=NULL)
    {
        printf("请输入学号:\n");
        scanf("%ld",&p1->sum);
        printf("请输入姓名:\n");
        scanf("%s",&p1->name);
        printf("请输入性别:\n");
        scanf("%s",&p1->sex);
        printf("请输入出生日:\n");
        scanf("%s",&p1->birth);
        printf("请输入数学成绩:\n");
        scanf("%d",&p1->score1.math_score);
        printf("请输入英语成绩:\n");
        scanf("%d",&p1->score1.English_score);
        printf("请输入c语言成绩:\n");
        scanf("%d",&p1->score1.c_score);
        printf("%-15ld%-10s%-10s%-20s%-5d%-5d%-5d\n",p1->sum,p1->name,p1->sex,p1->birth,p1->score1.math_score,p1->score1.English_score,p1->score1.c_score);
    }
    do
    {
        if(n==0)
        {
             head=p1;
             n+=1;
        }
        else
        {
            p2->next=p1;
            p2=p1;
        }
        printf("是否继续添加,“是”请按“1”,“否”请按“2”");
        scanf("%d",&m);
        do
        {
            printf("请重新输入");
            scanf("%d",&m);
        }while(m!=1&&m!=2);

        if(m==2)break;
        if(m==1)
        {


        p1=(struct student*)malloc(sizeof(struct student));
        printf("请输入学号:\n");
        scanf("%ld",&p1->sum);
        printf("请输入姓名:\n");
        scanf("%s",&p1->name);
        printf("请输入性别:\n");
        scanf("%s",&p1->sex);
        printf("请输入出生日:\n");
        scanf("%s",&p1->birth);
        printf("请输入数学成绩:\n");
        scanf("%d",&p1->score1.math_score);
        printf("请输入英语成绩:\n");
        scanf("%d",&p1->score1.English_score);
        printf("请输入c语言成绩:\n");
        scanf("%d",&p1->score1.c_score);
        }
        
    }while(1);
    p2->next=NULL;
    return head;
  

}
void display(struct student *head)
{    struct student *p;
     p=head;
     printf("%-15s%-10s%-10s%-20s%-5s%-5s%-5s\n","学号","姓名","性别","出生年月","数学","英语","c语言");
     do
     {
         printf("%-15ld%-10s%-10s%-20s%-5d%-5d%-5d\n",p->sum,p->name,p->sex,p->birth,p->score1.math_score,p->score1.English_score,p->score1.c_score);
         p=p->next;
     }while(p!=NULL);
         
}
void save(struct student *head)
{
    FILE *fp;
    stu *p1=head;
    if((fp=fopen("date.dat","a"))==NULL)
    {
        printf("无法打开文件");
        exit(0);
    }
    while(p1!=NULL)
    {
        fwrite (p1,sizeof(struct student),1,fp);
        p1=p1->next;
    }
    fclose(fp);
    printf("*********************保存成功**********************\n");
}
void cx1()
{
    long sum1;
    int n=0;
    FILE *fp;
    stu p1;
   
    printf("请输入学号");
    scanf("%ld",&sum1);
    if((fp=fopen("date.dat","rb"))==NULL)
    {
        printf("无法打开文件");
        exit(0);
    }
    while(!feof(fp))
    {
        fread(&p1,sizeof(struct student),1,fp);
        if(sum1==p1.sum)
        {
             printf("%-15ld%-10s%-10s%-20s%-5d%-5d%-5d\n",p1.sum,p1.name,p1.sex,p1.birth,p1.score1.math_score,p1.score1.English_score,p1.score1.c_score);
             n=1;
        }

    }
    if(sum1!=p1.sum&&n==0)
        printf("查无此人\n");
    fclose(fp);
}
void load()
{
    int n=0;
    FILE *fp;
    stu p,*p1,*p2,*head1;
    if((fp=fopen("date.dat","rb"))==NULL)
    {
        printf("无法打开文件");
        exit(0);
    }
    while(!feof(fp))
    {
        p1=(struct student*)malloc(sizeof(struct student));
        fread(&p,sizeof(struct student),1,fp);
        *p1=p;
        if(n==0)
        {
             head1=p1;
             p2=p1;
             n+=1;
        }
        else
        {
            p2->next=p1;
            p2=p1;
        }
    }
    fclose(fp);
    p2->next=NULL;
    display(head1);
    printf("*******************************读取数据*****************************************");


}

void del()
{   
    long sum;
    printf("请输入删除学生学号");
    scanf("%l",&sum);

}
int main()
{   
    struct student *head;
    load();
    meum();


    head=cz1();
    save(head);
    display(head);
    cx1();
   
    return 0;
}
这是半成品的代码,您可以看一下
2016-10-09 16:46
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:0 
fwrite (p1,sizeof(struct student),1,fp);
这个struct是可显示字符?
2016-10-09 17:25
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1728
专家分:3216
注 册:2015-12-2
得分:0 
数字应该都乱码,汉字和字母应该不会。
2016-10-09 20:12
linlulu001
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:20
帖 子:944
专家分:4047
注 册:2016-4-13
得分:5 
用fprintf吧,fwrite写的是二制内容,是给电脑看的。
2016-10-09 20:28
leodjh
Rank: 1
等 级:新手上路
帖 子:15
专家分:7
注 册:2016-8-13
得分:0 
谢谢各位大神
2016-10-10 08:48



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




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

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