![]() |
#2
rjsp2022-04-18 09:14
看不懂你那一系列 file1.seekp 是想实现什么功能,而你又什么都不肯说,连题目都不给。
![]() #define _CRT_SECURE_NO_WARNINGS // 进VC需要此宏 #include <iostream> #include <fstream> #include <type_traits> using namespace std; class CStudent { public: CStudent() = default; CStudent( const char* name, const char* id, float score=0.0f ); friend std::ostream& operator<<( std::ostream& os, const CStudent& stu ); private: char name_[10]; char id_[10]; float score_; }; CStudent::CStudent( const char* name, const char* id, float score ) { strncpy( name_, name, sizeof(name_) ); name_[sizeof(name_)-1]='\0'; strncpy( id_, id, sizeof(id_) ); name_[sizeof(id_)-1]='\0'; score_ = score; } std::ostream& operator<<( std::ostream& os, const CStudent& stu ) { os << '\n'; os << "学生信息如下:\n"; os << "姓名:" << stu.name_ << '\n'; os << "学号:" << stu.id_ << '\n'; os << "成绩:" << stu.score_; return os; } int main( void ) { // 仅当“可平凡复制”的类型,才可以使用std::memcpy复制或以std::ofstream::write()/std::ifstream::read()序列化自/到二进制文件的对象 static_assert( is_trivially_copyable_v<CStudent> ); CStudent students[] = { { "MaWenTao", "99001", 88 } , { "LiMing", "99002", 92 } , { "WangFang", "99003", 89 } , { "YangYang", "99004", 90 } , { "DingNing", "99005", 80 } }; fstream file( "student.dat", ios_base::in|ios_base::out|ios::binary|ios::trunc ); if( !file ) { cerr << " 文件创建失败.\n"; return 1 ; } if( !file.write((const char*)students,sizeof(students)) ) { cerr << " 文件写入失败.\n"; return 2 ; } // 将文件的 输入位置指示器 置于文件开始,进行读取 file.seekg( 0 ); for( CStudent stu; file.read((char*)&stu,sizeof(stu)); ) { cout << stu << '\n'; } } |
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
class CStudent
{
public:
CStudent(const char* name,const char* id, float score = 0);
void print();//输出
friend ostream& operator<<(ostream& os, CStudent& stu);
friend istream& operator>>(istream& is, CStudent& stu);
private:
char strName[10];
char strID[10];
float fScore;
};
CStudent::CStudent(const char* name,const char* id,float score)
{
strncpy_s(strName, name, strlen(strName)+1);
strncpy_s(strID, id,strlen(strID)+1);
fScore = score;
}
void CStudent::print()
{
cout << endl << "学生信息如下:" << endl;
cout << "姓名:" << strName << endl;
cout << "学号:" << strID << endl;
cout << "成绩:" << fScore << endl;
}
std::ostream& operator<<(ostream& os, CStudent& stu)
{
//使用定长格式,使每一个类的数据长度为24个字节
os.write(stu.strName, 10);
os.write(stu.strID, 10);
os.write((char*)& stu.fScore,4);//使float数据类型占4字节
return os;
}
std::istream& operator>>(istream& is, CStudent& stu)
{
char name[10], id[10];
is.read(name, 10);
is.read(id, 10);
is.read((char*)&stu.fScore, 4);//读入4字节数据作为float类型
strncpy_s(stu.strName, name, strlen(stu.strName)+1);
strncpy_s(stu.strID, id, strlen(stu.strID)+1);
return is;
}
void main()
{
CStudent stu1("MaWenTao", "99001", 88);
CStudent stu2("LiMing", "99002", 92);
CStudent stu3("WangFang", "99003", 89);
CStudent stu4("YangYang", "99004", 90);
CStudent stu5("DingNing", "99005", 80);
fstream file1;
file1.open("student.dat", ios::out | ios::in | ios::binary);
file1 << stu1 << stu2 << stu3 << stu4 << stu5;
CStudent* one = new CStudent("","");
const int size = 24;//每一个类的数据长度为24字节,与前呼应
file1.seekp(size * 4); file1 >> *one; one->print();
file1.seekp(size * 1); file1 >> *one; one->print();
file1.seekp(size * 2, ios::cur); file1 >> *one; one->print();
file1.close();
delete one;
}
只有本站会员才能查看附件,请 登录