Date: Tue, 16 Sep 1997 08:38:30 -0500 (CDT) From: Andrew Deren To: David Lenk cc: djgpp AT delorie DOT com Subject: Re: how do i do this In-Reply-To: <5vkqrg$ua@bgtnsc02.worldnet.att.net> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk On 16 Sep 1997, David Lenk wrote: > I am writing a program that creats a random file name and writes text into > a file and then saves it to disk. NO, it is not a virus, a friend told me > that this is a great way to learn c is to pick something reletivly hard and > stick with it until it has been totaly exploited. > > anyways, I need a small program that will make a random 8.3 format text > file (8.3 refers to the dos standard file length). The name generated > would then have to be stored in a variable so that it could be used in a > 'fprintf' command. I know how to write the file using fprintf but I have > no ideal of how to generate the random name. I have tried to make the > filename a random number but the program crashes with a GPF, and during > compilation i get pointer errors. > > Any help would be greatly apreaciated I know there's a C function for that, but I can't remember it's name, but you can use random number generator to do a random name: #include #include #include void dorandomname(char *filename) { int i; for (i=0; i<12; i++) { /* generate a character between A and Z */ filename[i] = rand() % 26 + 65; } /* put . (dot) in there */ filename[8] = '.'; /* put /0 at the end */ filename[12] = '\0'; } main() { char filename[15]; /* seed random number generator */ srandom(time(NULL)); dorandomname(filename); printf("File name is: %s", filename); } This simple program will print a random file name; note that the file name contains only alphabetic characters, if you want to use other characters, it's left as an exercise. But what ever filename the function generators, you should alwasy check if that file already exists. It might happen. > > -- > Thanks in advance for all your help! > > David Lenk, ~SamWise~ > lenkd AT pltpub DOT com > www.pltpub.com/lenkd > ,,, (0 0) +-------------oOO----(_)-------------------+ | Andrew Deren | | aderen AT eecs DOT uic DOT edu | | www.eecs.uic.edu/~aderen/ader/main.html | +-------------------------oOO--------------+ || || ooO Ooo