Creating a folder

This is a discussion on Creating a folder within the C Programming forums, part of the General Programming Boards category; Under Linux, how do i create a folder if it is not already created? I have this: sprintf(file_out, "./folder/%s", sorted[j]); ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    270

    Question Creating a folder

    Under Linux, how do i create a folder if it is not already created?

    I have this:
    sprintf(file_out, "./folder/%s", sorted[j]);

    where folder is a folder, but if the folder does not exist the code wont work. is there anyway to automatically create it if its not created?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by taurus View Post
    Under Linux, how do i create a folder if it is not already created?

    I have this:
    sprintf(file_out, "./folder/%s", sorted[j]);

    where folder is a folder, but if the folder does not exist the code wont work. is there anyway to automatically create it if its not created?
    The world "folder" is never used in the linux world.

    To create a directory you can use mkdir. mkdir is both a bash command, and a C library function (in sys/stat.h). You can use either one.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    Could I see an example of the use of the function mkdir in C

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    Could I see an example of the use of the function mkdir in C.
    I have tried:
    mkdir (const char *directory, mode_t mode);

    But not really sure, doesnt work.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    false alarm, found an example:
    int n = mkdir("./test", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code for creating a folder in your C:\ dive
    By jamietrent in forum C# Programming
    Replies: 4
    Last Post: 09-13-2009, 09:43 AM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  3. Creating a Folder
    By Junior89 in forum C++ Programming
    Replies: 9
    Last Post: 11-07-2004, 12:45 AM
  4. Creating a folder
    By bc17 in forum C++ Programming
    Replies: 7
    Last Post: 11-24-2002, 08:50 PM
  5. Creating a folder then dumping files into it!
    By ChrisMUK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 08-13-2002, 04:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21