Skip to content

Instantly share code, notes, and snippets.

@max-dark
Created April 11, 2017 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save max-dark/ec4c044e8ee4303300c91eb9436635e4 to your computer and use it in GitHub Desktop.
Save max-dark/ec4c044e8ee4303300c91eb9436635e4 to your computer and use it in GitHub Desktop.
#include <unistd.h>
//...
ssize_t buf_size = ...;
char * buffer = (char *) malloc(buf_size);
//....
ssize_t read_count = 0;
int file_in;
file_in = open(...);
int at_eof = 0;
while (!at_eof) {
read_count = read(file_in, buffer, buf_size);
switch(read_count) {
// eof
case 00: at_eof = 1; break;
// read error
case -1: {
switch(errno) {
// no data, retry read
case EINTR: case EAGAIN:break;
// fatal errors
default:
perror("read ");
abort();
break;
}
}
break;
// have some data - process it
default: {
// ...
at_eof = read_count != buf_size;
}
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment