home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 12 / CD_ASCQ_12_0294.iso / vrac / pclcjs.zip / RANDFILE.C < prev    next >
C/C++ Source or Header  |  1993-10-29  |  1KB  |  43 lines

  1. /* random_file
  2.    
  3.    accepts a file name mask, returns the name of a file randomly
  4.    selected from those meeting the mask filespec via the filename pointer
  5.    
  6.    returns an integer value equal to total number of files meeting this
  7.    filespec, or -1 if no files were found                                  */
  8.  
  9. #include <direct.h>
  10. #include <dos.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. int random_file(char *mask, char *filename)
  15.  
  16. {
  17.     /* Variables */
  18.     int counter, counter2;
  19.     
  20.      struct ffblk filedata;
  21.      
  22.      /* Code begins here */
  23.     /* Seed random number generator */
  24.     srand(10);
  25.     
  26.     if (findfirst(mask, &filedata, FA_NORMAL) == 0) {
  27.         counter++;
  28.         while (findnext(&filedata) == 0)
  29.                 counter++;
  30.         counter=rand() % counter + 1;
  31.                 
  32.         /* Reset file finder */
  33.         findfirst(mask, &filedata, FA_NORMAL);
  34.         for (counter2=1;counter2<counter;counter2++)
  35.             findnext(&filedata);
  36.         strcpy(filename,filedata.ff_name);
  37.         
  38.         return counter;
  39.     }
  40.     else 
  41.         return -1;
  42. }
  43.