Created
July 2, 2014 10:09
-
-
Save ochinchina/e6d65abc2508e528084a to your computer and use it in GitHub Desktop.
read bytes to a buffer from C++ std::istream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
std::size_t read_bytes( std::istream& in, char* buf, std::size_t len ) { | |
std::size_t n = 0; | |
while( len > 0 && in.good() ) { | |
in.read( &buf[n], len ); | |
int i = in.gcount(); | |
n += i; | |
len -= i; | |
} | |
return n; | |
} | |
int main( int argc, char** argv ) { | |
std::istringstream in( "this is a test stream"); | |
char buf[100]; | |
std::size_t n = read_bytes( in, buf, sizeof( buf )); | |
assert( n < 100 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use
rdbuf->sgetn
method: