2

I'm new to C programming so there is probably a simple solution for this but I trying to create multiple directories and files within those directories by using a loop in C. For example,

Directory1

  1. text1.txt
  2. text2.txt

Directory2

  1. text1.txt
  2. text2.txt

I haven't yet implemented the loop but I am trying to append the file name onto the folder so that I can create the file.

I have attached the code and I know that the error is in line 5 where I am trying to concatinate the string. Is there a way to create a variable to store the name of the directory and also append the file name to directory in order to create the file?

Thank you for your help.

Here is the code I have written so far

char folder[] = "directory1/";
mkdir(folder, 0750);

//Create text file in directory
fPointer = fopen(folder + "text.txt", "w");

for(int i = 0; i < textLength; i++){
    //Only return numbers from 0 - 25
    int rnum = rand() % 26;
    //Use uppercase ascii values therefore add 65
    text[i] = (char) (rnum +65);

    //Write to the file
    fprintf(fPointer,"%c",text[i]);
}
//Stop writing to text.txt and close connection
fclose(fPointer);
6
  • 3
    folder + "text.txt" That's not how strings and string concatenation in C works. Do some research about the strcat function, and get a good beginners book to read. Commented Oct 6, 2017 at 19:17
  • I know thats not how it works in C but I already tried the strcat function however it overwrote the folder variable value. I only want to append to the string temporarily. Commented Oct 6, 2017 at 19:22
  • Then you have to save the length of your first string, strcat() the file onto it and use that, then truncate it back to where it was, strcat() the next file, etc.,,, Commented Oct 6, 2017 at 19:25
  • Actually, there's a better function than strcat in this case I think: char temp[SOME_VALUE_LARGE_ENOUGH]; snprintf(temp, sizeof(temp), "%stext.txt", folder); That way you can easily incorporate loop iterator values into the string. Commented Oct 6, 2017 at 19:26
  • I also recommend you learn about toupper. Commented Oct 6, 2017 at 19:29

2 Answers 2

1

First C does not support a + operator in case of string. You need to use strcat() for concatenating string in C.One more thing that always look at stat for checking if the directory exists, and mkdir, to create a directory.

The following code works

#include<stdio.h>
#include<stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>


 int main(){

    char folder[] = "directory1/";
    char text[1024];
    struct stat st = {0};
    FILE *fPointer;

    if (stat(folder, &st) == -1) {
        mkdir(folder,0750);
    }

   //Create text file in directory
   strcat(folder,"text.txt");
   fPointer = fopen(folder, "w");
   int len=strlen(folder);

   for(int i = 0; i < len; i++){
      //Only return numbers from 0 - 25
      int rnum = rand() % 26;
      //Use uppercase ascii values therefore add 65
      text[i] = (char) (rnum +65);

      //Write to the file
      fprintf(fPointer,"%c",text[i]);
  }
  //Stop writing to text.txt and close connection
   fclose(fPointer);
   return 0;
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the advice about stat. I tried strcat but it overwrote the folder variable. I only needed it temporarily
You can use sprintf()
1

Regarding the first line in your example:

char folder[] = "directory1/";

The trailing "/" is not necessary to create the directory directory1.

The line:

fPointer = fopen(folder + "text.txt", "w");

is not doing what you expect it to do. C uses string functions to manipulate strings, such as concatenating 2 strings. (#include <string.h>)

Given a location such as:

char absoluteDir[] = "/user1/dir1/dir2/"; // copy to an absolute location, or
char relativeDir[] = "../dir1/";// will go one dir up from location and copy to dir1

and a file name created as:

char filename[] = "text.txt";

use one of the string functions, such as strcat or sprintf to concatenate the component strings into a location such as

char dirPathFileName[260];

For example:

sprintf(dirPathFileName, "%s%s", absoultDir, filename);

or

strcat(dirPathFileName, relativeDir);
strcat(dirPathFileName, filename);

These will create either:

"/user1/dir1/dir2/text.txt"

or

"../dir1/text.txt"
(Note: this requires call be made from a location where one directory 
 up contains a sub-direcotry named 'dir1`)

Which will work properly as the first argument of the function fopen

1 Comment

Yes, I already did it. But my reputation is too low for your up-click to be public

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.