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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:06:30+00:00 2026-06-12T16:06:30+00:00

Possible Duplicate: c++ how to create a directory from a path I’m trying to

  • 0

Possible Duplicate:
c++ how to create a directory from a path

I’m trying to create an .ini file in the application data folder so I can use settings. I gave it a try, but I couldn’t figure out how to check if it already exists, and if not, create the subdirectory and .ini file.

When I get the last error message it says “The system cannot find the path specified.”

#include <string>
using namespace std;

#include <Windows.h>
#include <Shlwapi.h>
#include <ShlObj.h>
#pragma comment(lib, "shlwapi.lib")

namespace Settings
{
    CIni        Ini;
    bool        Available = false;
    char        Directory[MAX_PATH];
    const char *IniFileName = "Settings.ini";

    void CheckError() {
        LPVOID lpMsgBuf;
        LPVOID lpDisplayBuf;
        DWORD dw = GetLastError(); 

        FormatMessage(
            FORMAT_MESSAGE_ALLOCATE_BUFFER | 
            FORMAT_MESSAGE_FROM_SYSTEM |
            FORMAT_MESSAGE_IGNORE_INSERTS,
            NULL, dw,
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
            (LPTSTR)&lpMsgBuf, 0, NULL
        );

        string msg = (LPTSTR)lpMsgBuf;
        Error(msg); // MessageBox
    }

    void Initialize() {
        // Get AppData directory
        if (SHGetFolderPath(0, CSIDL_COMMON_APPDATA, 0, 0, Directory) >= 0) {
            string fullpath;
            string subDir = "\\MyCompany\\MyProgram\\1.0\\";

            PathAppend(Directory, subDir.c_str());
            fullpath = Directory;
            fullpath += IniFileName;

            // If directory doesn't exist, create it.
            DWORD attrib = GetFileAttributes(Directory);
            if (!(attrib != INVALID_FILE_ATTRIBUTES && (attrib & FILE_ATTRIBUTE_DIRECTORY))) {

                if (CreateDirectory(Directory, NULL)) {
                    HANDLE file = CreateFile(
                        fullpath.c_str(), GENERIC_WRITE, 0, 0,
                        CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
                    );

                    if (file != INVALID_HANDLE_VALUE) {
                        CloseHandle(file);
                    } else {
                        CheckError();
                        return;
                    }
                } else {
                    CheckError();
                    return;
                }
            }

            Ini.SetPathName(fullpath.c_str());
            Available = true;
        }
    }
}

I checked and my call to CreateDirectory() is returning false.
How can I check if the file and directory exists, and if not then create them?

  • 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-12T16:06:31+00:00Added an answer on June 12, 2026 at 4:06 pm
    if (SHCreateDirectoryEx(NULL, Directory, NULL) || 
        GetLastError() == ERROR_ALREADY_EXISTS) 
       {
       HANDLE file = CreateFile(
          fullpath.c_str(), GENERIC_WRITE, 0, 0,
          CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
    
       if (file != INVALID_HANDLE_VALUE) 
         {
         CloseHandle(file);
         } 
         else if (GetLastError() != ERROR_FILE_EXISTS) 
           {
           CheckError();
           return;
           }
       } 
       else 
       {
       CheckError();
       return;
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C# Check if folder exist in directory and create them i have
Possible Duplicate: Create a file in /system directory First of all, i gave the
Possible Duplicate: How can I create directory tree in C++/Linux? Why mkdir fails to
Possible Duplicate: How can I create a temp file with a specific extension with
Possible Duplicate: Build Tar file from directory in PHP without exec/passthru Does PHP have
Possible Duplicate: Getting the application's directory from a WPF application I want to acess
Possible Duplicate: How can I perform full recursive directory & file scan? I can't
Possible Duplicate: Create provisioning profile in iphone application i developed my iphone app and
Possible Duplicate: Create Excel file in Java How to save output in excel format
Possible Duplicate: Create shortcut to console.log() In javascript we can easy assign functions to

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.