(no subject)

From: Sean P. Mountcastle (mountcas@pluto.ee.cua.edu)
Date: 01/26/95


	Many thanks for helping me with my last question (the guild
spec_proc).  If anyone cares it was the return 1; which was at the end
of the block of if statements checking race limits (it broke out of the
function and never reached the gain_level section).  Also my if statements
were wrong because they used if (something == (1 || 2 || 3 || 4)) which I
found out was bad form (but it gives no errors).  Oh, well, now I have a new
problem, I have tried MANY things with the segment of code included below
but I still cannot get it to do what I want.  I would like the code to generate
DIFFERENT 5 to 10 character 'words' and place them in a file.  The first problem
I have is that since I am using srand(time(NULL)); and the code executes more
than one times a second (or whatever srand uses), I am getting the same 'word'
printed over and over again.  Ideally I would like it to check the file before
it wrote to it to see if it came up with that 'word' before.  I used the rand
in the for loop to make sure it prints only readable characters.
(33 + (rand() % 93)) the chars 33 to 126 in ASCII.  I would rather not use
the goto statement, but I saw no other way (but then again it seems that the
goto statement isnt working, it still prints all the duplicate stuff over and
over).
	Thanks in advance for any help you could offer.
	This was done in TC in case anyone cares (and for those of you saying,
"What the hell does this have to do with CircleMUD?", I'm trying to generate
some keywords/passwords (?) for certain quests/special areas within the MUD.
Though it would be nice if I could generate words that still use printable
characts (but not as much) and more letters than numbers, so it looks more
like a real word....).

	Oh, well, thanks again.

	- Sean Mountcastle

   int handle, i;
   char string[12], *last[12], filename[9], *ch, *stri[12];
   int length, res;
   unsigned int mres;

   /*
    Create a file in the current directory and write randomly generated
    strings to it.  If the file already exists, it will be appended.
   */

   printf("Enter filename: ");
   gets(filename);

   if ((handle = open(filename, O_WRONLY | O_CREAT | O_APPEND,
		      S_IREAD | S_IWRITE)) == -1)
   {
      printf("Error opening file.\n");
      exit(1);
   }

   while (!kbhit())
   {
      start:
      srand(time(NULL));

      for(i=0; i<12; i++)
      {
	 string[i] = '\0';
      }

      for (i=0; i < (5 + (rand() % 3)); i++)
      {
	  *ch = (33 + (rand() % 93));
	  (string[i]) = *ch;
      }

	  strcat(string, "\r\n");
	  length = strlen(string);
	  *stri = string;

      if (stri != last)
      {
	 if ((res = write(handle, string, length)) != length)
	 {
	    printf("Error writing to the file.\n");
	    exit(1);
	 }
      }
      else
	 goto start;

      *last = string;
     /* strcpy(last, string);*/
      mres += res;
   }


   printf("Wrote %d bytes to the file.\n", mres);

   close(handle);
   return 0;
}



This archive was generated by hypermail 2b30 : 12/07/00 PST