1

I have an istream and I have to read it into a buffer. I could not find a way to figure out the read_len once eof is encountered? I cannot use get because my file does not have delimeters.

It seems that the only option is to read it character by character, is it really the only option?

char buffer[128];
while(is.good()) {
     is.read(buffer, sizeof(buffer));
     size_t read_len = sizeof(buffer);
     if (is.eof()) {
         read_len = xxxx;
     }
     process(buffer, read_len);
}
1
  • readsome? Commented Nov 11, 2021 at 15:26

1 Answer 1

5

You could check istream::gcount() which "Returns the number of characters extracted by the last unformatted input operation".

Example:

    while(is) {
         is.read(buffer, sizeof buffer);
         auto read_len = is.gcount();  // <- 
         if(read_len > 0)
             process(buffer, read_len);
         else
             break;
    }

You could also use istream::readsome() - but note: "The behavior of this function is highly implementation-specific." which may or may not be an issue.

Sign up to request clarification or add additional context in comments.

Comments

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.