Thread: Creating Folders using C

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    24

    Creating Folders using C

    maybe my question is quite simple, but although searching like wild I cannot
    find a solution.
    I just want to create a file, let's say to "c:\my folder\filename1.txt".

    Normally with append-option, the file is created if it doesn't exist. But I
    want also the folder to be created if it does not exist - now if the
    specified folder is not available, the file isn't created at all.

    Is there an additional option in fopen or orther function that could do exactly this?

    I am using Windows-7 OS and MS Visual C++ compiler. I want to do this using C language without using system() function.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    24
    I want to work using C language !!! can u give me a short example of how the above function works ??

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Ok, as you have been told before in your other post; working with directories is OS specific. The link provided to you by Tater shows you the windows API for creating a directory which is what you have asked for. Feel free to do some reading and learning on your own.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Just in case you forgot:

    Quote Originally Posted by CommonTater View Post
    No it would not require system() callls... The FindFirstFile(), FindNextFile(), CopyFile() etc calls that you would use are part of the standard Windows API and can be called directly from C code and will work on all versions of Windows since Win95.

    The portability issue is going to be problematic since each OS has it's own filesystem and thus it's own API calls for locating and enumerating files.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by parimal.s.dave View Post
    I want to work using C language !!! can u give me a short example of how the above function works ??
    This is a C function, it is part of the Windows API (Application Programming Interface) provided to give you access to operating system functions... just like the strings library adds strings to the basic language.

    Code:
    #include <windows.h>
    
    int main (void)
      {  char *NewDir = "coppers";
    
          CreateDirectory(NewDir,NULL);
    
          return 0; }
    It's just that easy...

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    24
    Hello Mr. anduril462,
    I know, probably you are a very good programmer .... or maybe I am talking to future bill gates...you might be working on this things for years...... but dude ...I have started working on this thigs for only a month or so ..... it will take time !!!!!! ok !!! I know my questions might be a little immatured!!!! but it requires patience to learn things !!!!!!! and I try to keep it !!!!!!! hope u will understand!!!!

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Are they responding to a signature?

  9. #9
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Exclamation marks are like cats. Having a few is fine, but more than about three makes you look completely unhinged.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    120
    you could also use the system() function in stdlib.h and use cmd line commands to check for, and make dirs. at least i think that would work.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by shiroaisu View Post
    you could also use the system() function in stdlib.h and use cmd line commands to check for, and make dirs. at least i think that would work.
    Why would you want to do that when the Windows API is ten times faster and has everything you need?

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Matticus View Post
    Are they responding to a signature?
    It would appear so...

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    120
    Quote Originally Posted by CommonTater View Post
    Why would you want to do that
    lol that question again, well, maybe the op would want a list of available possibilities, im just saying he can do that, im not saying it is the best way to do it. At least i know that when try to reach something i like to know every path available, for education porposes mainly. For instance, now, and because i brought this up, op knows that using the windows API is faster, see?? Win win

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by shiroaisu View Post
    lol that question again, well, maybe the op would want a list of available possibilities, im just saying he can do that, im not saying it is the best way to do it. At least i know that when try to reach something i like to know every path available, for education porposes mainly. For instance, now, and because i brought this up, op knows that using the windows API is faster, see?? Win win
    Ok, point taken...

    However, you could have also mentioned that you way would be slower...

  15. #15
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    system("mkdir") would also be portable (mkdir works on Linux too), would create intermediate directories and would cleanup after itself on failure. To do that (minus portability), your one line CreateDirectory just turned into 10.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with folders
    By FlyingIsFun1217 in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2006, 09:41 AM
  2. Reading filenames and creating folders
    By Bitphire in forum C++ Programming
    Replies: 2
    Last Post: 03-03-2005, 01:38 PM
  3. Creating Folders
    By X PaYnE X in forum Windows Programming
    Replies: 2
    Last Post: 09-21-2004, 04:18 AM
  4. Deleting folders and sub folders
    By Boomba in forum C++ Programming
    Replies: 30
    Last Post: 06-11-2003, 11:58 AM
  5. Creating Folders
    By Folder Help Man in forum C++ Programming
    Replies: 4
    Last Post: 07-08-2002, 10:26 PM