下班了,都辛苦了,帮小白调个简单的程序,谢谢
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class News{
public:
News ()
{number=0;name=string("unkonw");score1=0;score2=0;}
News(int value1,string value2,double value3,double value4);
int getnumber()
{ return number;}
double getscore1()
{ return score1;}
double getscore2()
{ return score2;}
string name;
private:
int number;
double score1,score2;
};
News::News(int value1,string value2,double value3,double value4)
{
number=value1;
name=value2;
score1=value3;
score2=value4;
}
int main(){
News student[3]={
News(1217,"周瑶",86.0,90.0),
News(1212,"刘畅",89.0,88.0),
News(1213,"李毅",93.0,98.0)};
ofstream outf("text.txt"); //向文件输入数据
int i;
if(!outf)
{ cout<<"cannot open";
exit(1);
}
outf<<"学号"<<' '<<' ';
outf<<"姓名"<<' '<<' '<<' ';
outf<<"高数"<<' '<<' ';
outf<<"线代"<<"\r\n";
for( i=0;i<=2;i++){
outf<<student[i].getnumber()<<' '<<' ';
outf<<student[i].name<<' '<<' '<<' ';
outf<<student[i].getscore1()<<' '<<' '<<' ';
outf<<student[i].getscore2()<<"\r\n";
}
ofstream fout("text.dat",ios::out|ios::binary|ios::app); //以二进制方式向文件输入数据
if(!fout)
{ cout<<"cannot open";
exit(1);
}
for( i=0;i<=2;i++)
{
fout.write(reinterpret_cast<char *>(&student[i]),sizeof(student[i]));
}
fout.close();
ifstream inf("text.dat",ios::in|ios::binary); //将数据再次输入内存
News stu[3];
for(i=0;i<=2;i++)
{
inf.read(reinterpret_cast<char *>(&stu[i]),sizeof(stu[i]));
cout<<stu[i].getscore1();
}
inf.close();
return 0;
}
[解决办法]
// my_test.cpp : Defines the entry point for the console application.
//
// #include "stdafx.h"
// #include "iostream"
// #include "MyLog.h"
// using namespace std;
//
//
// int main()
// {
// pr_begin();
//
// int i = 10;
// while (i--)
// {
// Sleep(1000);
// pr_debug("i am a student,i=%d\n",i);
// }
// pr_end();
//
// system("PAUSE");
// return 0;
// }
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class News{
public:
News ()
{number=0;name=string("unkonw");score1=0;score2=0;}
News(int value1,string value2,double value3,double value4);
int getnumber()
{ return number;}
double getscore1()
{ return score1;}
double getscore2()
{ return score2;}
string name;
int read_data(char* pbuff,int nlen)
{
//从pbuff读数据
//number,score1,score2,长度,字符串
number = *(int*)pbuff;
score1 = *(double*)(pbuff+sizeof(int));
score2 = *(double*)(pbuff+sizeof(int)+sizeof(double));
int nsize = *(int*)(pbuff+sizeof(int)+sizeof(double)+sizeof(double));
if (nsize>0&&nsize<50)//假设名字字节数(0--50之间)
{
char *pname = (pbuff+sizeof(int)+sizeof(double)+sizeof(double)+sizeof(int));
name.assign(pname,nsize);
}
else
{
cout<<"名字长度读取失败"<<endl;
}
return sizeof(int)+sizeof(double)+sizeof(double)+sizeof(int)+nsize;
}
int write_data(char* pbuff,int nlen)
{
//向pbuff里面写数据
*(int*)pbuff = number;
*(double*)(pbuff+sizeof(int)) = score1;
*(double*)(pbuff+sizeof(int)+sizeof(double)) = score2;
int nsize = name.size();
*(int*)(pbuff+sizeof(int)+sizeof(double)+sizeof(double)) = nsize;
char *pname = (pbuff+sizeof(int)+sizeof(double)+sizeof(double)+sizeof(int));
memcpy(pname,name.c_str(),nsize);
return sizeof(int)+sizeof(double)+sizeof(double)+sizeof(int)+nsize;
}
public:
int number;
double score1,score2;
};
News::News(int value1,string value2,double value3,double value4)
{
number=value1;
name=value2;
score1=value3;
score2=value4;
}
int main(){
News student[3]={
News(1217,"周瑶",86.0,90.0),
News(1212,"刘畅",89.0,88.0),
News(1213,"李毅",93.0,98.0)};
ofstream outf("text.txt"); //向文件输入数据
int i;
if(!outf)
{ cout<<"cannot open";
exit(1);
}
outf<<"学号"<<' '<<' ';
outf<<"姓名"<<' '<<' '<<' ';
outf<<"高数"<<' '<<' ';
outf<<"线代"<<"\r\n";
for( i=0;i<=2;i++){
outf<<student[i].getnumber()<<' '<<' ';
outf<<student[i].name<<' '<<' '<<' ';
outf<<student[i].getscore1()<<' '<<' '<<' ';
outf<<student[i].getscore2()<<"\r\n";
}
ofstream f_ou("text.dat",ios::binary); //以二进制方式向文件输入数据
if(!f_ou)
{ cout<<"cannot open";
exit(1);
}
for( i=0;i<=2;i++)
{
char pbuff[1024]={0};
int write_len = student[i].write_data(pbuff,1024);
f_ou.write(pbuff,write_len);
//fout.seekp(write_len,ios::cur);
}
f_ou.close();
ifstream f_in("text.dat",ios::binary); //将数据再次输入内存
News stu[3];
for(i=0;i<=2;i++)
{
News cls;
f_in.read((char*)&cls.number,sizeof(int));
f_in.read((char*)&cls.score1,sizeof(double));
f_in.read((char*)&cls.score2,sizeof(double));
int size = 0;
f_in.read((char*)&size,sizeof(int));
char szbuff[50];
f_in.read(szbuff,size);
cls.name.assign(szbuff,size);
//int read_len = student[i].read_data(pbuff,1024);
//inf.read(reinterpret_cast<char *>(&stu[i]),sizeof(stu[i]));
cout<<cls.getscore1()<<'\t'<<cls.name<<endl;
}
f_in.close();
return 0;
}