2 qq 29365517 qq_29365517 于 2017.01.14 09:43 提问

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

输入学生信息后,输出时的都是同一个数据,乱码。
程序分三个段
第一个输入函数
第二个输出函数
第三个调用主函数
程序段如下,请不吝赐教,谢谢!
#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个回答

u011261430
u011261430   2017.01.20 14: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;

}


u011261430
u011261430 回复qq_29365517: 额,是C++,你可以稍加修改成C的
10 个月之前 回复
qq_29365517
qq_29365517 谢谢!就是为了写入然后读出,请问这是用C++写的吗?
11 个月之前 回复
u011261430
u011261430   2017.01.14 11:46

大概看了看,发现写入有问题
1、“fwrite(&stu1,sizeof(struct stu),1,fp);”
ize_t fwrite(const void* buffer, size_t size, size_t count, FILE* stream);
注意:这个函数以二进制形式对文件进行操作,不局限于文本文件
返回值:返回实际写入的数据块数目
(1)buffer:是一个指针,对fwrite来说,是要获取数据的地址;
(2)size:要写入内容的单字节数;
(3)count:要进行写入size字节的数据项的个数;
(4)stream:目标文件指针;
(5)返回实际写入的数据项个数count。
应该是“fwrite(&stu1,1,sizeof(struct stu),fp);
这样写入是可以的
2、FILE fp;应该是FILE *fp;可能是复制掉了吧
3、文件路径反斜杠应该是两个吧

u011261430
u011261430 回复qq_29365517: 你这个是要干嘛的?存信息到文件,然后读取?
11 个月之前 回复
qq_29365517
qq_29365517 关于fwrite()百度百科下就有例子是我的那个结构的
11 个月之前 回复
qq_29365517
qq_29365517 FILE*fp 和反斜杠错误都是复制上来时候自动转换没了的,我的程序里是有的。
11 个月之前 回复
qq_29365517
qq_29365517 谢谢回复!但是fwrite里size是指要写入的结构大小,count是计数的意思,也就是几个结构,所以语法是"fwrite(&stu1,sizeof(struct stu),1,fp) 我尝试了你的写法,结果也是输出同一个乱码,
11 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!