Advertisement
pabouk

rndgen1.c

Aug 30th, 2013
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. /* generate a text file with random combinations of 4 characters
  6.    from a, b, c, d on each line */
  7. /* created for testing performance here: http://askubuntu.com/a/339182/67132 */
  8. /* compilation: make rndgen1 */
  9. /* detailed compilation: make rndgen1 CC=gcc CFLAGS="-W -Wall -pedantic" */
  10.  
  11. #define NLINES 200000000    /* number of output lines */
  12. #define CHPLINE 4       /* characters per line */
  13. #define PROGNAME "rndgen1"  /* program name */
  14. #define NPOSIB 4        /* output character possibilities */
  15. #define FCHAR 'a'       /* first output character possibility */
  16.  
  17. void errexit(const char *msg)
  18. {
  19.         fprintf(stderr, "%s: %s\n", PROGNAME, msg);
  20.         exit(EXIT_FAILURE);
  21. }
  22.  
  23. int main(int argc, char *argv[])
  24. {
  25.         int i, j;
  26.         FILE *fout;
  27.  
  28.         if(argc < 2) errexit("Too little arguments - You must specify an output file.");
  29.         if((fout = fopen(argv[1], "w")) == NULL) errexit("Error opening output file");
  30.         srand(time(NULL));
  31.  
  32.         for(i = 0; i < NLINES; i++) {
  33.                 for(j = 0; j < CHPLINE; j++) {
  34.                         if(putc(FCHAR + rand() % NPOSIB, fout) == EOF) {
  35.                 errexit("Error writting to file.");
  36.             }
  37.                 }
  38.                 if(putc('\n', fout) == EOF) errexit("Error writting to file.");
  39.         }
  40.  
  41.         fclose(fout);
  42.         return EXIT_SUCCESS;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement