qq_29365517 2017-01-14 01:43 采纳率: 100%
浏览 1056
已采纳

尝试编了学生工作管理系统程序,可是不知道哪儿错了!请不吝赐教!!

输入学生信息后,输出时的都是同一个数据,乱码。
程序分三个段
第一个输入函数
第二个输出函数
第三个调用主函数
程序段如下,请不吝赐教,谢谢!
#include
#include
#include
#include
void stuinfoinput()
{
FILE fp;
struct stu
{
int num;
char name[10];
char add[15];
int score;
}stu1;
if((fp=fopen("c:\my program\my downlod\student info.txt","wt+"))==NULL)
{
printf("can not open this file!\n");
exit(1);
}
printf("please input student number:\n0 is exit\n==========================================\n");
scanf("%d",&stu1.num);
while(stu1.num!=0)
{
printf("please input student's name,add,score\n");
scanf("%s,%s,%d",stu1.name,stu1.add,&stu1.score);
fseek(fp,sizeof(struct stu)
(stu1.num-1),0);
fwrite(&stu1,sizeof(struct stu),1,fp);
printf("please input student's number:\n");
scanf("%d",&stu1.num);
}
fclose(fp);
}
void stuinfooutput()
{
FILE *fp;
struct stu
{
int num;
char name[10];
char add[15];
int score;
}stu1;
if((fp=fopen("c:\my program\my downlod\student info.txt","wt+"))==NULL)
{
printf("can not open this file!\n");
exit(1);
}
printf("input student number:\n");
scanf("%d",&stu1.num);
fseek(fp,sizeof(struct stu) * (stu1.num-1),0);
fread(&stu1,sizeof(struct stu),1,fp);
printf("number: name: add: score:\n");
printf("%5d %s %s %5d\n",stu1.num,stu1.name,stu1.add,stu1.score);
fclose(fp);
}
int main()
{
int i;
printf("please input what you want:\n1 is input student info,\n2 is output student info\n0 is exit\n");
printf("====================================================\n");
scanf("%d",&i);
for(;i!=0;)
{
if(i==1)
{
stuinfoinput();
}
if(i==2)
{
stuinfooutput();
}
if(i==0)
{
exit(0);
}
printf("please input what you want:\n1 is input student info,\n2 is output student info\n0 is exit\n");
scanf("%d",&i);
}
return 0;
}

  • 写回答

2条回答 默认 最新

  • 仅仅学会简单 2017-01-20 06:53
    关注

    写了一个很粗糙的,不知道是什么需求

    // Test_two.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include
    #include
    #include
    using namespace std;
    #include

    typedef char i8;
    typedef unsigned char u8;
    typedef short i16;
    typedef unsigned short u16;
    typedef long int i32;
    typedef unsigned long u32;

    typedef struct STU_INFO
    {
    char name[10];
    char add[15];
    int score;
    }STU_INFO,*PSTU_INFO;

    std::map g_mapStu;

    void WriteStudentInfo(const int nStuNum, STU_INFO &sStu_Inof);//声明

    //初始化加载未处理订单
    void LoadOrderMap()
    {
    g_mapStu.clear();
    map::iterator it;

    FILE* pfile = NULL;
    string strDir = "C:\\Users\\Administrator\\Desktop\\新建文件夹\\student info.txt";
    if( 0 == fopen_s(&pfile,strDir.c_str(),"r+"))
    {
        fseek(pfile,0,SEEK_END);
        u32 dwsize = ftell(pfile);
        rewind(pfile);
    
        char* filebuffer = new char[dwsize];
        size_t nsize = fread(filebuffer, 1, dwsize, pfile);
        if (nsize != dwsize)
        {
            fclose(pfile);
            fopen_s(&pfile,strDir.c_str(),"r+");
            dwsize = nsize;
            delete []filebuffer;
            filebuffer = new char[dwsize];
            fread(filebuffer, 1, dwsize, pfile);
        }
    
        char *szsepLine = "\n";
        char *szsepRow = ",";
        char* sznexttokenLine = NULL;
        char* sznexttokenRow = NULL;
        STU_INFO sStuInfo;//学生信息
        int nStuNum = 0;
    
        char* szLine = strtok_s(filebuffer,szsepLine,&sznexttokenLine);
    
        while(NULL != szLine)//取一行
        {
            char* szRow = strtok_s(szLine,szsepRow,&sznexttokenRow);
            int i = 1;
            while(NULL != szRow)//nStuNum,name,add,score\n    //取一个数据
            {
                if (1 == i)
                {
                    nStuNum = atoi(szRow);
                }
                else if (2 == i)
                {
                    strcpy_s(sStuInfo.name, szRow);
                }
                else if (3 == i)
                {
                    strcpy_s(sStuInfo.add, szRow);
                }
                else if (4 == i)
                {
                    sStuInfo.score = atoi(szRow);
                }
    
    
                i++;
                szRow = strtok_s(NULL,szsepRow,&sznexttokenRow);
            }
    
            it = g_mapStu.find(nStuNum);
            if (it == g_mapStu.end())
            {
                g_mapStu.insert(pair<int ,STU_INFO>(nStuNum, sStuInfo));
            }
            else
            {
                g_mapStu.erase(it);
                g_mapStu.insert(pair<int ,STU_INFO>(nStuNum, sStuInfo));
            }
    
            szLine = strtok_s(NULL,szsepLine,&sznexttokenLine);
        }
    
        fclose(pfile);
    
        delete []filebuffer;
    }
    
    //删除错误数据
    for (it=g_mapStu.begin(); it!=g_mapStu.end();)
    {
        if (0 == it->first)
        {
            g_mapStu.erase(it++);
        }
        else
        {
            it++;
        }
    }
    
    //二次写入
    for (it=g_mapStu.begin(); it!=g_mapStu.end(); it++)
    {
        WriteStudentInfo(it->first, it->second);
    }
    

    }

    //学生信息写入
    void WriteStudentInfo(const int nStuNum, STU_INFO &sStu_Inof)
    {

    char chOrderBuf[1024] = "";//nStuNum,name,add,score\n
    sprintf_s(chOrderBuf, "%d,%s,%s,%d\n", nStuNum, sStu_Inof.name, sStu_Inof.add, sStu_Inof.score);
    
    string strDir = "C:\\Users\\Administrator\\Desktop\\新建文件夹\\student info.txt";
    FILE* pfile = NULL;
    if( 0 == fopen_s(&pfile,strDir.c_str(),"a+"))
    {
        fwrite(chOrderBuf,1,strlen(chOrderBuf),pfile);
        fclose(pfile);
    }
    else
    {
        cout<<"open OrderLogDir error"<<endl;
    }
    

    }

    //显示学生信息
    void ShowStudentInfo(const int nStuNum=0)
    {
    map::iterator it;

    if (0 == nStuNum)//显示全部
    {
        if (!g_mapStu.empty())
        {
            printf("***********student info***********\n");
            for (it=g_mapStu.begin(); it!=g_mapStu.end(); it++)
            {
    
                printf("Num:%d\n", it->first);
                printf("name:%s\n", it->second.name);
                printf("add:%s\n", it->second.add);
                printf("score:%d\n", it->second.score);
            }
            printf("*********************************\n");
        }
        else
        {
            printf("***********student info is empty***********\n");
        }
    }
    else
    {
        //查找某个学生
        it = g_mapStu.find(nStuNum);
        printf("*********************************\n");
        if (it!=g_mapStu.end())
        {
            //找到显示
            printf("Num:%d\n", it->first);
            printf("name:%s\n", it->second.name);
            printf("add:%s\n", it->second.add);
            printf("score:%d\n", it->second.score);
        }
        else
        {
            //未找到
            printf("**************Student is non existence**************\n");
        }
        printf("*********************************\n");
    }
    

    }

    //插入学生信息
    void InsertStudent(const int nStuNum, STU_INFO &sStu_Inof)
    {
    map::iterator it;
    it = g_mapStu.find(nStuNum);
    if (it!=g_mapStu.end())
    {
    //找到修改(可以考虑提示用户后再修改)
    strcpy_s(it->second.name, sStu_Inof.name);
    strcpy_s(it->second.add, sStu_Inof.add);
    it->second.score = sStu_Inof.score;
    }
    else
    {
    //未找到插入
    g_mapStu.insert(pair(nStuNum, sStu_Inof));
    }

    //直接写入文件保存
    WriteStudentInfo(nStuNum, sStu_Inof);
    

    }

    int _tmain(int argc, _TCHAR* argv[])
    {
    LoadOrderMap();//初始化加载学生信息

    int nCmd = 0;//命令参数
    
    int nStuNum = 0;
    while(true)
    {
        printf("please input what you want:\n1 is Insert student info,\n2 is Show student info\n0 is exit\n");
        printf("CMD:");
        scanf_s("%d", &nCmd);
    
        nStuNum = 0;
        if(1 == nCmd)
        {
            //stuinfoinput();
            STU_INFO sStu_info={0};
    
            printf("please input student's Num:");
            scanf_s("%d", &nStuNum);
            printf("please input student's name:");
            scanf_s("%s", sStu_info.name, sizeof(sStu_info.name));
            printf("please input student's add:");
            scanf_s("%s", sStu_info.add, sizeof(sStu_info.add));
            printf("please input student's score:");
            scanf_s("%d", &sStu_info.score);
    
            InsertStudent(nStuNum, sStu_info);
            printf("Insert student OK\n");
        }
        else if(2 == nCmd)
        {
            //stuinfooutput();
            printf("please input student's Num(input 0 to show all student):");
            scanf_s("%d", &nStuNum);
            ShowStudentInfo(nStuNum);
        }
        else if(0 == nCmd)
        {
            break;
        }
        //一般功能增、删、改、查
    
    }
    
    system("pause");
    return 0;
    

    }

    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题