Thread: Create folder from variable name

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    4

    Lightbulb Create folder from variable name

    Hi all

    I have searched for this answer but can't find any posts regarding this particular problem.

    I want to create a folder based on a string variable that I have assigned, but cannot figure out how. There are many examples using functions such as:
    system("MD NewFolder") or
    mkdir("NewFolder) etc

    However I want the name of the folder to be based on a string variable that I have assigned e.g.

    string fldname = "XYZ";
    mkdir(fldname);

    however this gives an error...
    main.cpp cannot convert `std::string' to `const char*' for argument `1' to `int mkdir(const char*)'

    Could someone please give me an idea of how I should go about this. Another point to note - I am writing this on a windows platform, but will then need to compile on UNIX so the system() function would need to be modified if I go with that solution.

    Thanks

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    The c_str() function returns a const char*
    Code:
    std::string fldname = "XYZ";
    mkdir(fldname.c_str());
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    4
    Thanks JaWib - exactly what I was looking for.

  4. #4
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    Quote Originally Posted by JaWiB
    The c_str() function returns a const char*
    Code:
    std::string fldname = "XYZ";
    mkdir(fldname.c_str());
    What is the header file u used. It said "mkdir" undeclare variable when i used it.
    Hello, testing testing. Everthing is running perfectly...for now

  5. #5
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    I believe mkdir is not a standard function. I think it's a UNIX function, and you have to include <sys/stat.h>.

    If you're on Windows, you can use the CreateDirectory function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global and static variable in a class delivered in a DLL
    By George2 in forum C++ Programming
    Replies: 16
    Last Post: 04-13-2008, 08:19 AM
  2. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM
  3. How to create a folder
    By andreas_nordman in forum C++ Programming
    Replies: 1
    Last Post: 06-14-2004, 06:25 AM
  4. An Idea I have...
    By TechWins in forum Tech Board
    Replies: 1
    Last Post: 07-25-2003, 05:49 AM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM