#std::istream::read doesn't return the last part of the file

5 messages · Page 1 of 1 (latest)

vestal plaza
#

Hello! I am using std::istream::read to read the contents of a file, N bytes at a time. I noticed that the last part of the file is not returned.

    string fname = "output/input";
    ifstream inputfile (fname);
    if (!inputfile.is_open()) {
        fprintf(stderr, "Failed to open input file, reason: %d\n", errno);
        exit(1);
    }

    vector<char> vec(seg_size - sizeof(int));
    while(inputfile.read(vec.data(), vec.size())) {
        vec.resize(inputfile.gcount());
        process_input_part(vec);
        vec.resize(seg_size - sizeof(int));
    }

relevant stackoverflow post: https://stackoverflow.com/questions/50491833/how-do-you-read-n-bytes-from-a-file-and-put-them-into-a-vectoruint8-t-using-it

Can you please tell me what I am doing wrong and how I can make sure that the last part of the file is also returned?

ripe islandBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

celest forum
#

The code looks ok, but the repetitive resizing is very bad for performance, make your process_input_part take in a vector and a count - gcount()

#

To view exactly what was read, dump it with cout but in hex mode