Thread: info: mkdir(const char *path)

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    3

    info: mkdir(const char *path)

    Hello :-)

    I'm an engineer (mechanic) so Cprogramming is not my job, and my question can sound silly
    to most of you. Please understand and thank you in advance.

    My problem is:

    I get a FileList.txt on argv[1], for example:

    C:\F00\Pippo_Folder\Pippo_Data\PippoData001.dat
    C:\F00\Robin_Folder\Robin_Data\RobinData005.dat
    C:\F00\Batman_Folder\Batman_Data\BatmanData100.dat
    etc..

    I want to fopen(_argv[1],"r") then fopen(first_file,"rb"); and that's OK, I can do that.

    My problem is that after reading the file I need to create a NewFile.dat with the same name
    and "same Path" but to a new folder, for example i need this kind of output:

    C:\F01\Pippo_Folder\Pippo_Data\PippoData001.dat
    C:\F01\Robin_Folder\Robin_Data\RobinData005.dat
    C:\F01\Batman_Folder\Batman_Data\BatmanData100.dat

    (note F00 changed to F01..)

    of course when i try to fopen(NewFile_in_NewPath,"wb") i get my warning
    "cannot open file.." .. beacuse the NewPath doesn't exist yet.(I think)

    So i tried to use the function mkdir(const char *path) but i cant get it right
    It seem not so easy( to me ) to automatically create folders from C code.


    Any tips ?
    ThankYou Paolo

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You should be able to do that with mkdir() - can you post the code where you try to create the path?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    probably you should create one folder at a time
    Firstly you create
    C:\F01

    Then
    C:\F01\Pippo_Folder

    And at last
    C:\F01\Pippo_Folder\Pippo_Data
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    I didnt write yet the code to build the string I need, at the moment ( just to try mkdir function ) I wrote this code taking the info from my compiler (DevC++)

    Code:
    #include <stdio.h>
    
    int main()
    {
       const char path[256] = "c:\\F01\\New";
       if (mkdir (path) != 0) printf("Fail Retry");
       return 0 ;
    }
    And simply doesn't create anything.
    I tried to find info or examples but the much info I get the more confused I am.
    Some examples I found where very tricky and full of flags; Is really this difficult?

    Thanks P

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    As I said - try to build path step by step
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, I take it that NEITHER "f01" nor "new" exists, right? You can't create multiple levels of directories with mkdir - I just tried it on my machine with your code, and it fails. If I change it to create only F01 then it works.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    Quote Originally Posted by vart View Post
    As I said - try to build path step by step
    :-) Yes It works :-)

    Thanks ! You make me Happy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  4. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM
  5. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 11:11 AM