Mail Archives: djgpp/1997/09/16/13:40:53
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 -