0

I apologize in advance for my lack of explanation because I am not good at English. I created a binary file that stores these objects. I want to read it and push back on the vector, but I don't know what to do.

class Player
{
    string name;
    int score;
    int id;
    size_t num; 
    char* p;
};

These functions were used when writing to binary files.

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

This is the operator overload function of the class. Below it is the main function, which is written in this way to read the file, but is not working poorly.

istream& operator>> (istream& is, Player& p)
{
    getline(is, p.name, '\0');
    is.read((char*)&p.score, sizeof(int));
    is.read((char*)&p.id, sizeof(int));
    is.read((char*)&p.num, sizeof(size_t));

    return is;
}

int main()
{
    ifstream in{ "Myfile", ios::binary };
    vector<Player> players(istream_iterator<Player>(in), {});
}

What should I do to read the file? I want to read the data of each object, but I think more data is being read than I want.

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

class Player
{
    string name;
    int score;
    int id;
    size_t num;
    char* p;

public:
    Player(): name{NULL}, score{0}, id{0}, num{0}, p{nullptr}
    {}

    Player(string name, int score, int id, size_t num) :
        name{ name }, score{ score }, id{ id }, num{ num }, p{ new char[num] }
    {}
    
    ~Player() 
    { 
        delete[] p; 
    }

    /*Player(const Player& other) : name{ other.name }, score{ other.score }, id{ other.id }, num{ other.num }, p{ new char[num] }
    {
        memcpy(p, other.p, num);
    }*/

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

    friend istream& operator>> (istream& is, Player& p);
};

istream& operator>> (istream& is, Player& p)
{
    getline(is, p.name, '\0');
    is.read((char*)&p.score, sizeof(int));
    is.read((char*)&p.id, sizeof(int));
    is.read((char*)&p.num, sizeof(size_t));

    return is;
}

int main()
{
    ifstream in{ "Myfile", ios::binary };
    vector<Player> players(istream_iterator<Player>(in), {});

    in.close();
}
109
  • 21
  • 1
  • _is not working poorly_ means _is working decently_. Probably you meant something else. As a general suggestion, you probably just want to use `std::copy`. – Enlico Apr 18 '21 at 06:32
  • By the way, your code is not compilable nor runnable. Please, provide a [repro]. – Enlico Apr 18 '21 at 06:36
  • Your last example doesn't even compile either. Please, adjust. – Enlico Apr 18 '21 at 06:45

1 Answers1

0

Given the scarce information you provided, I believe probably this what you're looking for:

    std::copy(
            std::istream_iterator<int>(in),
            std::istream_iterator<int>(),
            players.begin()
            );

You can #include <iterator> to have the std::istream_iterators available.

Enlico
  • 23,259
  • 6
  • 48
  • 102
  • I think it's because you don't have a file to read, and I don't have a detailed code because writing the file was provided by another program. So I only posted the code of the reading section. If you read like that, p.score, p.id, and p.num will have strange values. Is there any other way to read this? – 109 Apr 18 '21 at 07:00
  • @109, "it's because"? What's is "it"? – Enlico Apr 18 '21 at 08:58
  • This means that the code will not be compiled. I wanted to solve the problem, so I looked at other things, and https://stackoverflow.com/questions/14545120/how-do-i-read-data-from-textfile-and-push-back-to-a-vector I thought my situation was similar to the question here. However, if you read this and write a code similar to it, the file won't be read. – 109 Apr 18 '21 at 09:04
  • @109 i don't understand what you write, sorry. – Enlico Apr 18 '21 at 09:11
  • I think it's because I lack the ability to explain. I'm sorry. Earlier, you said that the code is not compiled. I think that's because I'm not creating a file, but I'm using the file I received. https://stackoverflow.com/questions/14545120/how-do-i-read-data-from-textfile-and-push-back-to-a-vector I think the question asked by the questioner on this link is similar to mine. I want to get the information of the object from the binary file and save it to the vector. However, when I run my code, 'name' stores the value, but not the other variables. – 109 Apr 18 '21 at 09:22