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 4581880
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:59:32+00:00 2026-05-21T20:59:32+00:00

So, I want to be ableto chdir into a directory, if it exists, if

  • 0

So, I want to be ableto chdir into a directory, if it exists, if not make the directory. If I am already in the directory, I just don’t need to do anything.

Example

if (cur_dir == "dir_name")
// do stuff
else if ("dir_name" not exist?)
   mkdir "dir_name"
   chdir "dir_name"
else
   chdir "dir_name"

I’ve been googling, I’ve come up with this so far:

if (chdir(Config::CONFIG_FOLDER_NAME) == 0)
{
    std::cout << "Network Config Directory not found...\n";
    std::cout << "Creating folder called " << Config::CONFIG_FOLDER_NAME << "\n";
    mkdir(Config::CONFIG_FOLDER_NAME, 0777);
}

I haven’t yet found a way to check to see what the current directory is.. (not the full path, which is what I did find.)

  • 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-05-21T20:59:33+00:00Added an answer on May 21, 2026 at 8:59 pm

    If you have Boost, you can use Boost.Filesystem:

    namespace fs = boost::filesystem;
    
    fs::path configFolder(Config::CONFIG_FOLDER_NAME);
    
    // Check if the current directory isn't already config folder
    if (!fs::equivalent(configFolder, fs::current_path())
    {
        // Create config folder if it doesn't exist
        if (!fs::exists(configFolder))
           fs::create_directory(configFolder);
    
        // Change working directory to config folder
        fs::current_path(configFolder);
    }
    

    By the way, if you’re just planning to read a config file, you shouldn’t need to change the working directory. Just read directly, using an absolute path. In Boost.Filesystem, you’d do that this way:

    fs::path configFilePath = configFolder;
    configFilePath /= "config.file";
    
    // read configFilePath
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to take an image that i have already captured
I want to be able to replicate only the folder structure (not the contents)
I want to make a custom UIBarButtonItem to represent the Done button. I want
I want to establish a standard script file that is imported into python at
I just wrote a simple DirectShow Filter (which inherits from CTransformFilter). But I want
I want to please know how to place individual 'articles' into individual CSS 'div's...??
I'm just not understanding on how to create a lambda expression instead of using
I want to be able to put an object into one of several shards
I'm making a small file editor and the only kicker is I don't want
I kind of want to make a game.... so I want able to dynamically

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.