How to read large binary files and store them in containers

  Kiến thức lập trình

Solve the problem using the ++ STL. The member variables of Player look like this

class Player {
string name; // Name, length[3, 15]
int score; // Score
size_t id; // id, there can be duplicates
size_t num; // number of bytes in free store
unique_ptr<char[]> p; // Memory freed in free store
}

[data file]
Get the file “2024 STLmaterials.exe” and run it. – The executable file is built in X64, Release mode. Check that the “2024 STL Assignment File” is created in the folder. – The file contains 200’0000 (2 million) Player objects. Open the file as a binary (ios::binary) and use the member function write to create the 200’0000 Player objects.
and used the member function write to write 2 million Player objects.

void Player::write( ostream& os ) {
os.write((char*)this, sizeof(Player));
os.write((char*)p.get(), num);
}

[ Challenges to be solved ]

  • You must read the file only once to solve the task.
  • You must solve the problems in this order 1. Save all the Player information stored in the file to a container. Print the last Player’s
    information for the last Player in the file in the following format
    Name:yirdoohnmrmpueo, ID:1346650, Score:6420173 , Resource:77
    Saved characters:cfpvmbryqmnanbccsuhtaisueouvtjlhjszojaewcacjzrljgzfyc
    xavzdxydhdvqqal

I don’t know how to read the file and get the 2 million into the container.

New contributor

App Key is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT