#include #include #include #define READBUFSIZE 128 int main(int argc, char **argv) { FILE *f; char readbuf[READBUFSIZE]; size_t readlen; // Take the filename from command line argument if (argc != 2) { fprintf(stderr, "usage: ./fread_example filename\n"); return EXIT_FAILURE; // defined in stdlib.h } // Open, read, and print the file f = fopen(argv[1], "rb"); // "rb" --> read, binary mode if (f == NULL) { fprintf(stderr, "%s -- ", argv[1]); perror("fopen failed"); return EXIT_FAILURE; } // Read from the file, write to stdout. while ((readlen = fread(readbuf, 1, READBUFSIZE, f)) > 0) fwrite(readbuf, 1, readlen, stdout); // Test to see if we encountered an error. if (ferror(f)) { perror("fread failed"); return EXIT_FAILURE; } fclose(f); return EXIT_SUCCESS; // defined in stdlib.h }