home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / TEXT / UTILITY / WISDOM2.ZIP / WISDOM.C next >
Encoding:
C/C++ Source or Header  |  1991-03-25  |  1.7 KB  |  81 lines

  1. /*
  2.     Yet another fortune cookie type program.  I wrote this because
  3.     I wanted one that didn't need a pointer file, didn't require
  4.     ANSI.SYS loaded, didn't write files to disk and didn't crash
  5.     '386 machines, and I couldn't find one.  The data file should
  6.     be a standard text file with each fortune starting with a #
  7.     character.  The fortunes have been collected from a variety of
  8.     sources.
  9.  
  10.     Peter Summers - 24/3/91
  11. */
  12.  
  13.  
  14. #include <conio.h>
  15. #include <io.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <values.h>
  19. #include <time.h>
  20. #include <fcntl.h>
  21. #include <sys\stat.h>
  22.  
  23. main (int argc, char *argv[])
  24.   {
  25.   int    handle,numread;
  26.   long    offset;
  27.   char    *extension,*datafile,*buffer,*outstring;
  28.  
  29.   datafile=calloc(128,sizeof(char));
  30.  
  31.   if (argc>1)
  32.     strcpy(datafile,argv[1]);
  33.   else
  34.     {
  35.     strcpy(datafile,argv[0]);
  36.     extension=strrchr(datafile,'.');
  37.     if (extension==NULL)
  38.       {
  39.       cprintf("%s","No '.' found in argc[0] in WISDOM");
  40.       exit(1);
  41.       }
  42.     else
  43.       strcpy(extension,".DAT");
  44.     }
  45.  
  46.   buffer=calloc(4097,sizeof(char));
  47.  
  48.   handle=open(datafile,O_RDONLY|O_BINARY,S_IREAD);
  49.  
  50.   if (handle==-1)
  51.     {
  52.     cprintf("Can't open data file %s in WISDOM",datafile);
  53.     exit(1);
  54.     }
  55.  
  56.   randomize();
  57.   offset=((long)random(MAXINT)*MAXINT+random(MAXINT))%filelength(handle);
  58.  
  59.   lseek(handle,offset,SEEK_SET);
  60.   numread=read(handle,buffer,4096);
  61.  
  62.   if (numread==-1)
  63.     {
  64.     cprintf("Can't read from data file %s in WISDOM",datafile);
  65.     exit(1);
  66.     }
  67.  
  68.   if (numread<4096)
  69.     {
  70.     lseek(handle,0,SEEK_SET);
  71.     read(handle,&buffer[numread],4096-numread);
  72.     }
  73.  
  74.   outstring=strchr(buffer,'#')+1;
  75.  
  76.   *strchr(outstring,'#')=0;
  77.  
  78.   textcolor(3);
  79.   cprintf("%s",outstring);
  80.   }
  81.