madrugadas25845
madrugadas25845

Reputation: 25

read from binary file and store to a buffer

Can somebody tell if this is correct? I try to read from binary file line by line and store it in a buffer? does the new line that it stores in the buffer delete the previous stored line?

        ifs.open(filename, std::ios::binary);
        for (std::string line; getline(ifs, line,' '); )
                {
                    ifs.read(reinterpret_cast<char *> (buffer), 3*h*w);

                }

Upvotes: 0

Views: 346

Answers (1)

Starl1ght
Starl1ght

Reputation: 4493

For some reason you are mixing getline which is text-based reading, and read(), which is binary reading.

Also, it's completely unclear, what is buffer and what's it size. So, here is a simple example for you to start:

ifs.open(filename, std::ios::binary); // assume, that everything is OK

constexpr size_t bufSize = 256;
char buffer[bufSize];
size_t charsRead{ 0 };
do {
    charsRead = ifs.read(buffer, bufSize)
    // check if charsRead == 0, if it's ok
    // do something with filled buffer.
    // Note, that last read will have less than bufSize characters,
    // So, query charsRead each time.
} while (charsRead == bufSize);

Upvotes: 1

Related Questions