Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 25th, 2011  |  syntax: None  |  size: 1.11 KB  |  views: 99  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char **argv) {
  5.   char *filename = NULL;
  6.   unsigned char *buf;
  7.   FILE *h;
  8.   int blocksize = 4096;
  9.  
  10.   if (argc > 1) {
  11.     filename = argv[1];
  12.     if (argc > 2) {
  13.       blocksize = atoi(argv[2]);
  14.       if (argc > 3) {
  15.         fprintf(stderr, "Extra arguments ignored.\n");
  16.       }
  17.     }
  18.   }
  19.   if (filename == NULL) {
  20.     fprintf(stderr, "Usage: %s filename blocksize\n", argv[0][0] ? argv[0] : "program");
  21.     exit(EXIT_FAILURE);
  22.   }
  23.   buf = malloc(blocksize);
  24.   if (!buf) {
  25.     fprintf(stderr, "Not enough memory for %d byte buffer\n", blocksize);
  26.     exit(EXIT_FAILURE);
  27.   }
  28.   h = fopen(filename, "rb");
  29.   if (!h) {
  30.     fprintf(stderr, "Cannot open input file: %s\n", filename);
  31.     free(buf);
  32.     exit(EXIT_FAILURE);
  33.   }
  34.   while (!feof(h)) {
  35.     size_t chk;
  36.     printf("reading %d bytes ...", blocksize);
  37.     fflush(stdout);
  38.     chk = fread(buf, 1, blocksize, h);
  39.     printf(" done: %lu bytes read                   PRESS ENTER", (long unsigned)chk);
  40.     fflush(stdout);
  41.     getchar();
  42.   }
  43.   fclose(h);
  44.   free(buf);
  45.   return 0;
  46. }