Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7726393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:12:12+00:00 2026-06-01T05:12:12+00:00

How do I make a directory/folder with c++. I’ve tried to use the mkdir()

  • 0

How do I make a directory/folder with c++. I’ve tried to use the mkdir() without success. I want to write a program that cin’s a variable and then uses this variable to create sub-directory’s and files with in those.
my current code. It says the + operator in the mkdir() says error no operand

char newFolder[20];

cout << "Enter name of new project without spaces:\n";
cin >> newFolder;
string files[] = {"index.php"};
string dir[] = {"/images","/includes","/includes/js","/contact","about"};

for (int i = 0; i<=5; i++){
mkdir(newFolder + dir[i]);
ofstream write ("index/index.php");
write << "<?php \n \n \n ?>";
write.close();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:12:13+00:00Added an answer on June 1, 2026 at 5:12 am

    You need to #include <string>, the std::string operators are defined in that header.

    The result of the expression newFolder + dir[i] is a std::string, and mkdir() takes a const char*. Change to:

    mkdir((newFolder + dir[i]).c_str());
    

    Check the return value of mkdir() to ensure success, if not use strerror(errno) to obtain the reason for failure.

    This accesses beyond the end of the array dir:

    for (int i = 0; i<=5; i++){
        mkdir(newFolder + dir[i]);
    

    there are 5 elements in dir, so legal indexes are from 0 to 4. Change to:

    for (int i = 0; i<5; i++){
        mkdir(newFolder + dir[i]);
    

    Usestd::string for newFolder, rather than char[20]:

    std::string newFolder;
    

    Then you have no concern over a folder of more than 19 characters (1 required for null terminator) being entered.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make a program that lists files contained in a certain directory.
In Unix, can I run make in a directory without cd 'ing to that
I have a directory on a linux box that I want to make publicly
I want to make a directory and all subfolders and files password protected so
I'm trying to make a script that will go into a directory and run
I use the below to create folder in document directory , my question is
I want to make a listview of files in folder,but it doesn't work. What's
I have a little problem, I need to make directory from my c code
Follow up to this question about GNU make : I've got a directory, flac
I'm using (GNU) Make in my project. I'm currently putting one makefile per directory

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.