You are here:

C++/File Directories and Extensions


I have a program that I have been working on that creates 10 random files and writes a random number of words to each file.  Each file is a different size.  However, it is writing to my code directory and I would like it to write to a specific folder on my C drive called Test Files.  I would like these files to be text files as well.  I am having difficulty with this and would appreciate any help.  Thanks for your time.  I have included my code below.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdlib.h>
#include <time.h>
#include <conio.h>
#include <direct.h>

int getFileNames (int mod);

void rword (char *word);

int main()
       // variables
       char name[5];
       int RnD, i, b;
       int fileNames = 0;
       while (fileNames <= 10)
         char word[7];

         // initialize random number generator

         // set random file name
         for (i = 0; i <= 5; i++)
         RnD = getFileNames(b);
         name[i] = RnD;

         // create random file
         FILE * writtenFile = fopen(name, "wt");

         // set random size
         int z = rand () % 50;
         int y = rand () % 100000 * z;

         // write random words of file using random size
         for (int x = 0; x < y; x++)
         // function call

         // write words to file
         fprintf (writtenFile, "%s ", word);

         // close random file
         fclose (writtenFile);

         // show file names on screen

         // increment counter

       // key press to exit screen
       return 0;

int getFileNames (int mod)
       int RndNmb = 0;
       srand((time(NULL) * mod));
       RndNmb = 'a' + rand() % 26;
       return RndNmb;


void rword (char *word)
       // set length to no more than 7 characters
       int len = rand () % 6 + 1;

       // set array to zero
       word [len] = 0;

       // set number values to a matching ASCII value
       while (len) word [--len] = 'a' + rand () % 26;


First, there is a problem forming the file name:

         for (i = 0; i <= 5; i++)
         RnD = getFileNames(b);
         name[i] = RnD;

If you're going to use i <= 5, then char name[5] isn't big enough.  You also have to leave room and store a final NUL (0).  So make name[7] and use name[6] = 0; to terminate it.

In order to use C:\Test Files you have 2 options:

1.  Build the names like C:\Test Files\abcxyz.txt
2.  Switch to folder C:\Test Files and just use the name.

Since you need to add .txt to files, I'll show you option #1.  One problem with #2 is you would want to switch the folder back when you're done and this adds complexity.

So, define char fullName[24]; (24 is definitely big enough).  Before the fopen, do (just as typed):

strcpy( fullName, "C:\\Test Files\\" );
strcat( fullName, name );
strcat( fullName, ".txt" );

If name is abcxyz you'll have fullName = "C:\Test Files\abcxyz.txt"
Using this fullName with fopen will work fine.

Otherwise, the program looks pretty good - good job!



All Answers

Answers by Expert:

Ask Experts


Bill A


I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.


I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

BS Computer Engineering

©2016 All rights reserved.