输入学生信息后,输出时的都是同一个数据,乱码。
程序分三个段
第一个输入函数
第二个输出函数
第三个调用主函数
程序段如下,请不吝赐教,谢谢!
#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;
}
尝试编了学生工作管理系统程序,可是不知道哪儿错了!请不吝赐教!!
- 写回答
- 好问题 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;
#includetypedef 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;
}
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 iOS 自定义输入法-第三方输入法
- ¥15 很想要一个很好的答案或提示
- ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
- ¥15 怀疑手机被监控,请问怎么解决和防止
- ¥15 Qt下使用tcp获取数据的详细操作
- ¥15 idea右下角设置编码是灰色的
- ¥15 全志H618ROM新增分区
- ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
- ¥15 NAO机器人的录音程序保存问题
- ¥15 C#读写EXCEL文件,不同编译