输入学生信息后,输出时的都是同一个数据,乱码。
程序分三个段
第一个输入函数
第二个输出函数
第三个调用主函数
程序段如下,请不吝赐教,谢谢!
#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;
}
写了一个很粗糙的,不知道是什么需求
// 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、“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、文件路径反斜杠应该是两个吧