home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / alde_c / misc / zed / goodie.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-10-10  |  3.1 KB  |  114 lines

  1. #include <stdio.h>
  2. #include <time.h>
  3.  
  4. /*
  5.  *    goodie.c -- Goofy little program to print random selections from
  6.  *        a list contained in a 'goodiefile'.
  7.  *
  8.  *        Version 1.01, Copyright (C) 1987 by David Sandberg
  9.  *
  10.  *        To use this program, you will want to create a file
  11.  *        called 'goodies.fil' in the \etc directory (you may change
  12.  *        the filename by changing the first 'if', but I highly recommend
  13.  *        using an absolute pathname at least, so the program can find
  14.  *        it no matter where it is executed from), which may contain any
  15.  *        number of goofy quips and quotes you like. If the quip or
  16.  *        quote is more than one line long, each line after the first
  17.  *        must begin with a space character (ASCII 32) - this is how the
  18.  *        program determines that it is a continuation line.
  19.  *        EXAMPLE:
  20.  
  21. "Now is the time for all good men to
  22.  come to the aid of their country."
  23.  
  24.  *        will be interpreted correctly as one contiguous entry, while
  25.  
  26. "Now is the time for all good men to
  27. come to the aid of their country."
  28.  
  29.  *        will be treated incorrectly as two separate entries.
  30.  *
  31.  *        Note that you should have a carriage return after the last
  32.  *        entry in your 'goodiefile', so that the program can properly
  33.  *        read the last entry. On an editor such as 'vi', this means
  34.  *        a blank line will appear at the end of the list of 'goodies'.
  35.  *
  36.  *        If you run the program dozens of times in a few minutes, you 
  37.  *        may start to see patterns arise. Since the randomize function
  38.  *        is driven by the time of day, this seems unavoidable. The
  39.  *        algorithm is quite suitable for less rigorous applications,
  40.  *        however - I use it in my AUTOEXEC.BAT file to give myself a
  41.  *        random greeting upon powerup, with good results.
  42.  *
  43.  *        This program was compiled using Microsoft C version 4.00. To
  44.  *        compile it with a different C compiler will likely require
  45.  *        mods to the random and time functions. If anyone does so, or
  46.  *        if someone comes up with a better random device, I'd like to
  47.  *        see the results (simply out of curiosity) - write me on
  48.  *        the C Station, (612) 938-4809.
  49.  *
  50.  *        D. Sandberg -- 10/10/87
  51.  */
  52.  
  53. main()
  54.  
  55. {
  56.     FILE *goodiefile;
  57.     int filelen, line_no;
  58.  
  59.     randomize();
  60.     if ((goodiefile = fopen("/etc/goodies.fil", "r")) == NULL) {
  61.     printf("goodiefile missing.\n");
  62.     exit(0);
  63.     }
  64.     filelen = find_size(goodiefile);
  65.     line_no = (rand() % filelen) + 1;
  66.     pick_line(goodiefile, line_no);
  67. }
  68.  
  69. randomize()
  70.  
  71. {
  72.     long *timeval, time();
  73.  
  74.     time(timeval);
  75.     srand((int)*timeval);
  76. }
  77.  
  78. find_size(goodiefile)
  79. FILE *goodiefile;
  80.  
  81. {
  82.     char buf[83];
  83.     int filelen = 0;
  84.  
  85.     while (!feof(goodiefile)) {
  86.     fgets(buf, 82, goodiefile);
  87.     if (buf[0] != ' ')
  88.         ++filelen;
  89.     }
  90.     rewind(goodiefile);
  91.     return(filelen-2);
  92. }
  93.  
  94. pick_line(goodiefile, line_no)
  95. FILE *goodiefile;
  96. int line_no;
  97.  
  98. {
  99.     int i=0;
  100.     char buf[83];
  101.  
  102.     while (i<line_no) {
  103.     fgets(buf, 82, goodiefile);
  104.     if (buf[0] != ' ')
  105.         ++i;
  106.     }
  107.     printf("\n%s", buf);
  108.     fgets(buf, 82, goodiefile);
  109.     while (buf[0] == ' ') {
  110.     printf("%s", buf);
  111.     fgets(buf, 82, goodiefile);
  112.     }
  113. }
  114.