Thread: Create folders

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

    Create folders

    Hey! I need to make my program to make a directory named Data.
    I tried with this:

    Code:
    system("md Data");
    But it resulted in that the program crashed.
    Any help would be appreciated.

    Also an explanation of why my program crashes would be cool

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    On a windows machine, you should be able to use _mkdir (or _wmkdir if you are using unicode strings).

    http://msdn2.microsoft.com/en-us/lib...zw(vs.71).aspx

    --
    Mats

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    10
    Okay great thx, I've read the description and it seems like the thing I need.
    Just wondering, how do I use it? Haha sorry if being annoying.

    But didn't really get it, I guess it's a library I have to include.
    so.. do I need to download the header file or is it just like #include <direct.h> or something?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Groogy View Post
    Okay great thx, I've read the description and it seems like the thing I need.
    Just wondering, how do I use it? Haha sorry if being annoying.

    But didn't really get it, I guess it's a library I have to include.
    so.. do I need to download the header file or is it just like #include <direct.h> or something?

    Something like this:
    Code:
    #include<direct.h>
    
    int main(void) {
       int res;
    
       res = _mkdir("foo");
       if (!res)
          res = _mkdir("foo/bar");
       if (res)
          printf("failed to create directory/ies\n");
       return 0;
    }
    --
    Mats

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    10
    Hmm.. I've tried around a little and commenting in and out stuff in my text and found out that the reason of my program crashing, wasn't cause of the folder creation.
    Is it okay for me to post the code here? Or do I need to make a new thread for that?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As it's sort of related, I'd say post it here.

    By the way, the above "_mkdir" function is certainly preferrable to the system("md ...") solution, as the latter is much more expensive (it creates a new process, then runs the md command, and exits).

    --
    Mats

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    10
    Hmm okay great.

    Well here's kinda some background.
    I'm 16 years old, going on a Game Developing high school (think I translated that right).
    This ain't homework as we don't got this kind of stuff yet, but I'm trying to be ahead of schedule or something.

    Well anyway, I'm trying to make a kind of Online Strategy Game, (kinda like the Web based you see all over the web)
    And this part is supposed to create the needed files for the "Server". Some of these files is supposed to be placed in a directory called Data.

    Code:
    void install()
    {
         /* Create all critical files which is needed to be able to run the server */
         /* Create a "Default" settings.xml file*/
         FILE *install_file = fopen("settings.xml", "w");
         fprintf(install_file, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n", stdout);
         fprintf(install_file, "<server>\n", stdout);
         fprintf(install_file, "\t<name>PA-Server01</name>\n", stdout);
         fprintf(install_file, "\t<version>0.1</version>\n", stdout);
         fprintf(install_file, "\t<updaterate>1</updaterate>\n", stdout);
         fprintf(install_file, "</server>\n", stdout);
         fprintf(install_file, "<admin>\n", stdout);
         fprintf(install_file, "\t<username>adminstrator</username>\n", stdout);
         fprintf(install_file, "\t<password>default</password>\n", stdout);
         fprintf(install_file, "\t<securitykey>00001111 10101010 01010101 11110000</securitykey>\n", stdout);
         fprintf(install_file, "</admin>\n", stdout);
         fclose(install_file);
         /* Create the file for SERVERSTATE*/
         install_file = fopen("state.info", "r");
         fprintf(install_file, "CLS", stdout);
         fclose(install_file);
         /* Create xml list for accounts */
         _mkdir ("Data");
         install_file = fopen("Data/accounts.xml", "w");
    }

    I started out with C++, but I learned that C should be faster.
    So I'm kinda new to C, but do get some things of it. So my guess is, that it's failing with fclose? And after using fclose with my FILE pointer, I have to create a new pointer?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I haven't looked much at your code, but
    Code:
         install_file = fopen("state.info", "r");
         fprintf(install_file, "CLS", stdout);
         fclose(install_file);
    doesn't look too good.
    Why are you WRITING to a file that is opened for reading? Did you mean "r+"?
    What's "stdout" got to do with anything? You are not printing the argument anyways.

    You are also not checking any of the fopen() to see if it worked - so if there's a problem, you will probably crash due to accessing a NULL pointer.

    Finally, I don't agree with
    I started out with C++, but I learned that C should be faster.
    C or C++ are both compiled languages. If you know roughly what you are doing, the code will be fast. If you don't know what you're doing, the code will not be fast - language has little to do with that. Also, if you know C++, it will help you get the application running faster than if you use a language you don't know.

    Sure C++ can easily cause all sorts of slowness because someone don't understand how to write good code (or use the language features in the right way) - it's probably a bit easier to do this wrong in C++ than in C - but neither are "sure to be fast"....

    --
    Mats

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    10
    Man I must have missed that "r"! Damn me! Thought I had scanned the whole function after any logical errors made by me. It was supposed to be "w".
    And no I'm not testing if it succeeded with opening, because foremost, I'm only planning on letting myself using it. There's no fancy layout for the server so that many different people can use it.
    You need to know what to do else you won't be able to work with the server.

    And the part with C being faster, a friend told me that which he has said from his own experience.
    When C++ uses classes, it has to load those classes from the memory, and list the functions, load them, THEN run the operations in the function. Afterwards it unloads it all again, and etc. etc.

    Kinda hard for me to explain, as I don't really know how I'm supposed to translate it from Swedish to English
    But this is kinda off-topic, if you want to discuss it you are free to mail me at [email protected]

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Groogy View Post
    Man I must have missed that "r"! Damn me! Thought I had scanned the whole function after any logical errors made by me. It was supposed to be "w".
    And no I'm not testing if it succeeded with opening, because foremost, I'm only planning on letting myself using it. There's no fancy layout for the server so that many different people can use it.
    You need to know what to do else you won't be able to work with the server.

    And the part with C being faster, a friend told me that which he has said from his own experience.
    When C++ uses classes, it has to load those classes from the memory, and list the functions, load them, THEN run the operations in the function. Afterwards it unloads it all again, and etc. etc.

    Kinda hard for me to explain, as I don't really know how I'm supposed to translate it from Swedish to English
    But this is kinda off-topic, if you want to discuss it you are free to mail me at [email protected]
    I think this is a very valid discussion, as it relates to your project and C/C++ is what this forum is about.

    When you use a class, it does indeed have a "funciton table" (if you use the virtual keyword) which is used to dereference the methods of that class. But the slowness of C++ is more related to the way that people tend to write code in C++ with functions that call member functions in the base class, and particularly long chains of constructor calls. But if we write C++ in a sensible way, and avoid using methods that cause long chains of calls that eventually does very little, it's not that bad.

    If you want to PM me with the Swedish, så kan jag översätta (I'll translate).... ;-)

  11. #11
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Groogy View Post
    And no I'm not testing if it succeeded with opening, because foremost, I'm only planning on letting myself using it. There's no fancy layout for the server so that many different people can use it.
    You should *always* check return codes, whether it's for you or not. It's just good programming practice and if you don't it *will* come back one day and kick you in the nads!
    You've been warned ;-)

    QuantumPete

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by QuantumPete View Post
    You should *always* check return codes, whether it's for you or not. It's just good programming practice and if you don't it *will* come back one day and kick you in the nads!
    You've been warned ;-)

    QuantumPete
    Case in point: The code trying to open a non-existing file failed, because it was using "r" instead of "w", which meant that the return was NULL, the next call to fprintf then crashes because of the NULL dereference!

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't create child windows
    By OnionKnight in forum Windows Programming
    Replies: 4
    Last Post: 04-10-2011, 04:13 PM
  2. Create a file from c++ program
    By Dan17 in forum C++ Programming
    Replies: 2
    Last Post: 05-08-2006, 04:25 PM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  4. How to create a file association program?
    By eShain in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2006, 12:15 PM
  5. How to Create reference to an array in C++
    By shiv_tech_quest in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2002, 10:01 AM