www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/16/13:40:53

Date: Tue, 16 Sep 1997 13:40:36 -0400 (EDT)
From: "Art S. Kagel" <kagel AT ns1 DOT bloomberg DOT com>
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.D-G.3.91.970916133556.22085C-100000@dg1>
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.

The first problem is that you cannot pass a filename to fprintf!  The
first argument to fprintf is a (FILE *) not a (char *)filename.  You
must pass the filename to fopen which will return a (FILE *) to pass
to fprintf, fwrite, fread, etc.  Hence:

      int MyRandomNumber;
      char filename[256];
      FILE *file;

      sprintf( filename, "%s.fil", MyRandomNumber );
      file = fopen( filename, "w" );
      fprintf(file,"This is how to write to a file using streams.\n"); 
      fflush( file );
      fclose( file );

Your name generation is probably OK.

Art S. Kagel, kagel AT bloomberg DOT com

- Raw text -


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