#include #include #include #include #include int main(int argc, char ** argv){ if(argc < 2){ printf("Usage: %s filename\n", argv[0]); return 1; } int fd = open(argv[1], O_RDONLY); printf("Our fd is %d\n", fd); if(fd < 0){ perror(argv[1]); return 1; } struct stat sb; fstat(fd, &sb); char *contents = (char*)malloc(sb.st_size); ssize_t readlen = read(fd, contents, sb.st_size); if(readlen == sb.st_size) printf("We read the expected amount of bytes\n"); else printf("We read %d bytes, expected %d\n", readlen, sb.st_size); size_t third = readlen / 3; contents[third * 2] = 0; puts(contents + third); close(fd); return 0; }