www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/16/09:37:23

Date: Tue, 16 Sep 1997 08:38:30 -0500 (CDT)
From: Andrew Deren <aderen AT eecs DOT uic DOT edu>
To: David Lenk <lenkd AT pltpub DOT com>
cc: djgpp AT delorie DOT com
Subject: Re: how do i do this
In-Reply-To: <5vkqrg$ua@bgtnsc02.worldnet.att.net>
Message-ID: <Pine.SUN.3.95.970916082637.1127B-100000@ernie.eecs.uic.edu>
MIME-Version: 1.0

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 <stdio.h>
#include <stdlib.h>
#include <time.h>

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
 


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019