文件写入/读取不工作

File Writing / Reading is not working

本文关键字:工作 读取 文件      更新时间:2023-10-16

我最近采用了一种注入到进程的新方法,除了配置读取/文件读取之外,一切都很好。这是我当前使用的代码:

string ReadIniKey(string section, string key){
    string iniPath = GetDocuments()/*This returns the correct value)*/ + "My Program\Config.ini";
    if (!ifstream(iniPath)){ ofstream file(iniPath); WriteINIDefaults(); }
    char retVal[255];
    GetPrivateProfileString((LPCWSTR)section.c_str(), (LPCWSTR)key.c_str(), (LPCWSTR)"", (LPWSTR)retVal, 255, (LPCWSTR)iniPath.c_str());
    return retVal;
}
void WriteIni(string section, string key, string value){
    string iniPath = GetDocuments()/*This returns the correct value)*/ + "My Program\Config.ini";
    if (!ifstream(iniPath)) ofstream file(iniPath);
    WritePrivateProfileString((LPCWSTR)section.c_str(), (LPCWSTR)key.c_str(), (LPCWSTR)value.c_str(), (LPCWSTR)iniPath.c_str());
}

它没有读取文件,创建文件或向现有文件写入任何内容。我被注入到一个64位进程中,有任何可能的修复吗?我主要关心的是GetPrivateProfileString/writepprivateprofilestring部分。

EDIT:在发布模式下编译它会创建文件,但是GetPrivateProfileString/writepprivateprofilestring不会对文件做任何事情

构造函数ifstream()不接受c++ string。取const char*,即Cchar/string。但是如果你正在使用C++11,那么在ifstream()构造函数中使用string应该不会有问题。

因此将ifstream(iniPath)更改为ifstream(iniPath.c_str())或使用C样式字符串

我得到它固定使用GetPrivateProfileStringA &writepprivateprofilestringa代替GetPrivateProfileString &WritePrivateProfileString

文件读写不正常