c++ - WinAPI - 如果 .ini 文件不存在则创建它

标签 c++ winapi file-io settings ini

<分区>

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

我正在尝试在应用程序数据文件夹中创建一个 .ini 文件,以便我可以使用设置。我试了一下,但不知道如何检查它是否已经存在,如果不存在,则创建子目录和 .ini 文件。

当我收到最后一条错误消息时,它说“系统找不到指定的路径。”

#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;
        }
    }
}

我检查了一下,我对 CreateDirectory() 的调用返回了 false。 如何检查文件和目录是否存在,如果不存在则创建它们?

最佳答案

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;
   }

关于c++ - WinAPI - 如果 .ini 文件不存在则创建它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12839025/

相关文章:

c++ - 根据其成员子对象之一的地址计算对象的地址

c++ - 调用 lambda 的语义操作

winapi - 混合两个 pcm 流的音频

c++ - RegisterDeviceNotification 返回 NULL 但仍收到通知

python - 读取大 CSV 后跟 `.iloc` 切片列时出现 Pandas MemoryError

C++ 客户端套接字仅接收消息的第一个字母

c++ - 线程本地存储和 iOS

perl 进程之间的 perl Win32 信号处理

c - 在 C 中,如何将文本文件的不同行保存到不同的变量中

linux - Linux 是否保证文件内容在 close() 后刷新到磁盘?