| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 编程论坛
共有 454 人关注过本帖
标题:关于一个简单的成绩管理系统。信息总显示乱码
只看楼主 加入收藏
firewater
Rank: 1
等 级:新手上路
帖 子:47
专家分:4
注 册:2014-7-13
结帖率:87.5%
  问题点数:0  回复次数:0   
关于一个简单的成绩管理系统。信息总显示乱码
开始是先创建一个文件,然后输入数据,输入完数据后保存数据在磁盘中,可是却出现了乱码。  当打开这个文件时,显示信息,显示的也是乱码。
主要是看这两个函数   //声明保存学生信息的函数 void save(struct student *current, FILE *file);    //声明添加学生信息函数 struct student *add(struct student *head);
 


程序代码:


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN sizeof(struct student)     //学生信息结构的长度
#define MAXNAMES 10      //存储名字的字节
#define MAXSEX 2           //汉语的性别需要2个字节
#define MAXSTUS 50      //最多存储的学生信息
#define MAXFILENAME 50

struct score               //成绩的结构体
{
    float C;
    float math;
    float chinese;
};

//学生信息的结构体
struct student         
{
    int IDnum;
    char name[MAXNAMES];
    int class;   
    struct score student_score;
    struct student * next;        //指向链表的下一个结构
};


//判断学号是否存在  0是不存在,1是存在
int judge(struct student * head, int IDnum);
//声明添加学生信息函数
struct student *add(struct student *head);               
//声明删除学生信息函数
struct student *del(struct student *head);              
//声明显示学生信息列表函数
void show_info(FILE * file);                        
//声明排序函数
void sort(struct student *head);                       
//声明修改函数
void modify(struct student *head);                     
//声明查找函数
void find(struct student *head);   
//声明保存学生信息的函数
void save(struct student *current, FILE *file);
                              
int count = 1;   //计算学生的数量

//主函数
int main(void)
{
    struct student * head = NULL;      //链表的头指针,先声明指向NULL
   
//进入界面
    while (1)
    {
        int flag = 0;  
        printf("------------------------------------------\n");
        printf("                  开始界面                \n");
        printf("------------------------------------------\n");
        printf("一:创建文件\n");
        printf("二:打开文件\n");
        printf("------------------------------------------\n");
        printf("输入数字1或2选择:");
        fflush(stdin);
        scanf("%d", &flag);

        //创建文件
        if (1 == flag)   
        {
            char new_file_name[MAXFILENAME];   //新文件的名字
            int flag_creat = 0;     //是否退出创建文件选项的标志
            FILE * new_file_pointer;  //指向新文件的指针

            printf("请输入创建的文件名(请勿与磁盘中的文件重名):");
            fflush(stdin);             //清空键盘缓冲区
            scanf("%s", new_file_name);
            while (NULL == (new_file_pointer = fopen(new_file_name,"wb+")))
            {
                printf("error:创建失败\n");
                printf("请重新输入新的文件名:");
                fflush(stdin);
                scanf("%s", new_file_name);
            }
            while (0 == flag_creat)
            {
                int select = 0;
                printf("--------------------------------\n");
                printf("①添加学生信息\n");
                printf("②保存信息\n");
                printf("③退出\n");
                printf("--------------------------------\n");
                printf("请选择操作项:");
                fflush(stdin);
                scanf("%d", &select);
                //添加学生信息
                if (1 == select)
                {
                    head = add(head);
                    printf("数据添加成功\n");
                    system("pause");
                }
                //保存信息
                else if (2 == select)
                {
                    rewind(new_file_pointer);
                    save(head, new_file_pointer);     //往新建文件中保存数据,直接传递头指针
                    printf("保存成功\n");
                    system("pause");
                }
                //退出
                else if (3 == select)
                {
                    flag_creat = 1;
                }
                else
                {
                    printf("请输入数字1,2或3.\n");
                    system("pause");
                }
                system("cls");
            }
        }        
        //打开已有文件
        else if (2 == flag)  
        {
            char file_name[MAXFILENAME];
            int flag_open = 0;     //是否退出选项的标志
            FILE * file_pointer;  //指向文件的指针

            printf("请输入要打开文件的文件名:");
            scanf("%s", file_name);
            while (NULL == (file_pointer = fopen(file_name, "ab+")))
            {
                printf("error:打开失败\n");
                printf("请重新输入文件名:");
                fflush(stdin);
                scanf("%s", file_name);
            }
            while (0 == flag_open)
            {
                int select = 0;
                //打开已有文件后的界面
                printf("输入数字1,2,3,4......进行选择\n");
                printf("------------------------------------------\n");
                printf("①添加学生数据\n");
                printf("②显示成绩列表\n");
                printf("③删除学生\n");
                printf("④排序\n");
                printf("⑤保存数据\n");
                printf("⑥读取数据\n");
                printf("⑦修改\n");
                printf("⑧退出系统\n");
                printf("------------------------------------------\n");
                printf("请选择操作项:");
                fflush(stdin);
                scanf("%d", &select);
                //添加学生信息
                if (1 == select)
                {
                    head = add(head);
                    printf("数据添加成功\n");
                    system("pause");
                }
                //显示信息列表
                else if (2 == select)
                {
                    show_info(file_pointer);
                    printf("已全部显示\n");
                    system("pause");
                }
                else if (3 == select)
                {
                }
                else if (4 == select)
                {
                }
                else if (5 == select)
                {
                    rewind(file_pointer);
                    save(head, file_pointer);     //往新建文件中保存数据,直接传递头指针
                    printf("保存成功\n");
                    system("pause");
                }
                else if (6 == select)
                {
                }
                else if (7 == select)
                {
                }
                //退出
                else if (8 == select)
                {
                    flag_open = 1;
                }
                else
                {
                    printf("请输入数字1,2,3,4......进行选择\n");
                    system("pause");
                }
                system("cls");
            }
        }
        
        else
        {
            printf("请输入1或2\n");
            system("pause");
        }
        system("cls");
    }




    system("pause");
    return 0;
}





//添加学生信息,建立链表
struct student *add(struct student *head)
{
   
    int temp_IDnum = 0;
    struct student * prev, *current;
    current = (struct student *)malloc(LEN);
    current->next = NULL;
    current->IDnum = 0;    //对新的节点的IDnum初始化为0
    if (NULL == head)
    {
        head = current;     //往新文件中添加学生信息
    }
    else
    {
        prev = head;             //往已有文件中添加学生信息
        while (prev->next != NULL)
        {
            prev = prev->next;     //找到链表的结尾
            count++;
        }
        count++;
        prev->next = current;    //将新的节点加到链表中
    }

    //添加学生信息
    printf("\n当学号为0时则停止添加,回到主界面\n");
    printf("---------请输入学生信息---------\n");
    printf("请输入第%d个学生的学号:", count);
    //fflush(stdin);   //清空键盘缓冲区
    scanf("%d", &temp_IDnum);
    while (0 != temp_IDnum)
    {   
        while (1)
        {
            int judge_flag = 0;            
            judge_flag = judge(head, temp_IDnum);              //判断学号是否重复
            if (0 == judge_flag)   //0是不存在,1是存在
            {
                break;
            }
            else    //存在
            {
                printf("该学号已录入信息,请重新输入学号:");
                //fflush(stdin);   //清空键盘缓冲区
                scanf("%d", &temp_IDnum);
            }
        }
        current -> IDnum = temp_IDnum;
        printf("请输入第%d个学生的姓名:", count);
        //fflush(stdin);
        scanf("%s", &(current->name));
        printf("请输入第%d个学生的班级:", count);
        //fflush(stdin);      //清空键盘缓冲区
        scanf("%d", &(current -> class));
        printf("C语言的分数:");
        //fflush(stdin);
        scanf("%f", &(current->student_score.C) );
        printf("数学的分数:");
        //fflush(stdin);
        scanf("%f", &(current->student_score.math));
        printf("语文的分数:");
        //fflush(stdin);
        scanf("%f", &(current->student_score.chinese));
        count++;    //统计学生数量
        prev = current;
        current = (struct student *)malloc(LEN);      //创建新的节点
        current->next = NULL;
        current->IDnum = 0;    //对新的节点的IDnum初始化为0
        prev->next = current;
        printf("\n");
        printf("请输入第%d个学生的学号:", count);
        //fflush(stdin);
        scanf("%d", &temp_IDnum);
    }

    return head;
}

//判断学号是否存在  0是不存在,1是存在
int judge(struct student * head, int temp_IDnum)
{
    int judge_flag = 0;
    struct student * current;
    current = head;
    do
    {
        if (current->IDnum == temp_IDnum)
        {
            judge_flag = 1;
            break;
        }
        current = current->next;
    } while (current != NULL);
    return judge_flag;
}

//定义保存信息的函数
void save(struct student *current, FILE * file)   //如果是选择新建的文件,则current接收新链表的头指针
{                                                //如果是原来存在的文件,则current接受最先加入节点的指针
    while (NULL != current)
    {
        fwrite(current, LEN, 1, file);
        current = current->next;
    }
    fclose(file);
}

//定义显示信息列表的函数
void show_info(FILE * file)
{
    rewind(file);
    struct student * show_head, * current;
    show_head = (struct student *)malloc(LEN);
    current = show_head;
    while (!feof(file))
    {
        fread(current, LEN, 1, file);
        current->next = (struct student *)malloc(LEN);
    }
    while (NULL != show_head)
    {
        printf("%-5d%-10s%-10d%-6f%-6f%-5f\n", show_head->IDnum,
                                                show_head->name,
                                                show_head->class,
                                                show_head->student_score.C,
                                                show_head->student_score.math,
                                                show_head->student_score.chinese);
        show_head = show_head->next;
    }
}
搜索更多相关主题的帖子: 管理系统  current  color  信息  file  
2015-05-10 21:07







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

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