0

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);

                }

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

i have to read a ppm file and store it to a buffer. the size of the buffer has to be height*weight*3. So, do you think this method is fast for this? Thanks
method is absolutely ok, you just need it to adjust it for your ppm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.