C++;GetEnvironmentVariable()的完整路径写入文件错误 我是C++的NoOB,但是想学。我有一个小程序,可以在Windows中将一些信息写入我的\etc\hosts;我通过GetEnvironmentVariable()获取%WINDIR%变量,如果我手动放置完整路径,一切正常,但是当我用WINDIR变量替换时,我的代码没有编译。我知道我做得不对 #include <windows.h> #include <ios> #include <fstream> char buffer[1000]; int main() { GetEnvironmentVariable("WINDIR",(char*)&buffer,sizeof(buffer)); std::ofstream log; log.open("%s\\system32\\drivers\\etc\\hosts", buffer); log << "127.0.0.1 domain.com\n" << std::endl; return 0; } #包括 #包括 #包括 字符缓冲区[1000]; int main(){ GetEnvironmentVariable(“WINDIR”,(char*)&buffer,sizeof(buffer)); std::流测井; 打开(“%s\\system32\\drivers\\etc\\hosts”,缓冲区); log

C++;GetEnvironmentVariable()的完整路径写入文件错误 我是C++的NoOB,但是想学。我有一个小程序,可以在Windows中将一些信息写入我的\etc\hosts;我通过GetEnvironmentVariable()获取%WINDIR%变量,如果我手动放置完整路径,一切正常,但是当我用WINDIR变量替换时,我的代码没有编译。我知道我做得不对 #include <windows.h> #include <ios> #include <fstream> char buffer[1000]; int main() { GetEnvironmentVariable("WINDIR",(char*)&buffer,sizeof(buffer)); std::ofstream log; log.open("%s\\system32\\drivers\\etc\\hosts", buffer); log << "127.0.0.1 domain.com\n" << std::endl; return 0; } #包括 #包括 #包括 字符缓冲区[1000]; int main(){ GetEnvironmentVariable(“WINDIR”,(char*)&buffer,sizeof(buffer)); std::流测井; 打开(“%s\\system32\\drivers\\etc\\hosts”,缓冲区); log,c++,compiler-errors,environment-variables,C++,Compiler Errors,Environment Variables,您需要像这样将字符串连接在一起: LPTSTR windir[MAX_PATH]; LPTSTR fullpath[MAX_PATH]; GetWindowsDirectory(windir, MAX_PATH); if(PathCombine(fullpath, windir, _T("system32\\drivers\\etc\\hosts")) != NULL) { std::ofstream log; log.open(buffer, ios_base::ate);

您需要像这样将字符串连接在一起:

LPTSTR windir[MAX_PATH];
LPTSTR fullpath[MAX_PATH];
GetWindowsDirectory(windir, MAX_PATH);
if(PathCombine(fullpath, windir, _T("system32\\drivers\\etc\\hosts")) != NULL) {
    std::ofstream log;
    log.open(buffer, ios_base::ate);
    log << "127.0.0.1   domain.com\n" << std::endl;
}
LPTSTR windir[MAX_PATH];
LPTSTR全路径[最大路径];
GetWindowsDirectory(windir,最大路径);
if(PathCombine(fullpath、windir、\u T(“system32\\drivers\\etc\\hosts”)!=NULL){
std::流测井;
打开(缓冲区,ios_base::ate);

log您需要像这样将字符串连接在一起:

LPTSTR windir[MAX_PATH];
LPTSTR fullpath[MAX_PATH];
GetWindowsDirectory(windir, MAX_PATH);
if(PathCombine(fullpath, windir, _T("system32\\drivers\\etc\\hosts")) != NULL) {
    std::ofstream log;
    log.open(buffer, ios_base::ate);
    log << "127.0.0.1   domain.com\n" << std::endl;
}
LPTSTR windir[MAX_PATH];
LPTSTR全路径[最大路径];
GetWindowsDirectory(windir,最大路径);
if(PathCombine(fullpath、windir、\u T(“system32\\drivers\\etc\\hosts”)!=NULL){
std::流测井;
打开(缓冲区,ios_base::ate);
日志
open(“%s\\system32\\drivers\\etc\\hosts”,buffer);
open不理解格式字符串。您使用的
%s
没有意义。了解

试着这样做:

GetEnvironmentVariable("WINDIR",buffer,sizeof(buffer));
strcat(buffer, "\\system32\\drivers\\etc\\hosts");
std::ofstream log;
log.open(buffer.str().c_str(), ios_base::ate);   
open(“%s\\system32\\drivers\\etc\\hosts”,buffer);
open不理解格式字符串。您使用的
%s
没有意义。了解

试着这样做:

GetEnvironmentVariable("WINDIR",buffer,sizeof(buffer));
strcat(buffer, "\\system32\\drivers\\etc\\hosts");
std::ofstream log;
log.open(buffer.str().c_str(), ios_base::ate);   

流的
无法为您设置路径格式。您需要单独设置,例如:

#include <windows.h>
#include <ios>
#include <fstream>

char buffer[1000] = {0};
int main() {
    GetEnvironmentVariable("WINDIR",buffer,sizeof(buffer));
    strcat(buffer, "\\system32\\drivers\\etc\\hosts");
    std::ofstream log;
    log.open(buffer, ios_base::ate);
    log << "127.0.0.1   domain.com\n" << std::endl;
    return 0;
}
#包括
#包括
#包括
字符缓冲区[1000]={0};
int main(){
GetEnvironmentVariable(“WINDIR”,buffer,sizeof(buffer));
strcat(缓冲区“\\system32\\drivers\\etc\\hosts”);
std::流测井;
打开(缓冲区,ios_base::ate);

流的log
无法为您设置路径格式。您需要单独设置路径格式,例如:

#include <windows.h>
#include <ios>
#include <fstream>

char buffer[1000] = {0};
int main() {
    GetEnvironmentVariable("WINDIR",buffer,sizeof(buffer));
    strcat(buffer, "\\system32\\drivers\\etc\\hosts");
    std::ofstream log;
    log.open(buffer, ios_base::ate);
    log << "127.0.0.1   domain.com\n" << std::endl;
    return 0;
}
#包括
#包括
#包括
字符缓冲区[1000]={0};
int main(){
GetEnvironmentVariable(“WINDIR”,buffer,sizeof(buffer));
strcat(缓冲区“\\system32\\drivers\\etc\\hosts”);
std::流测井;
打开(缓冲区,ios_base::ate);

日志我认为应该可以工作,但现在我得到了“C:\Dev Cpp\include\string.h函数'void*memcpy(void*,const void*,size_t')的参数太少”我交换了函数这应该工作得更好,也更安全。提供“PathCombineW()”函数的头文件是什么?因为我得到了“undeclared”错误。尝试禁用W。我应该先编译代码,然后再回答^^^尝试使用PathCombine并得到相同的错误,我知道这肯定是一个包含。我认为应该可以工作,但现在我得到了“C:\Dev Cpp\include\string.h太少的参数,无法实现函数'void*memcpy(void*,const void*,size_t')”我交换了这个函数,它应该工作得更好,也更安全。提供“PathCombineW()”函数的头文件是什么?因为我得到了“undeclared”错误。尝试阻止W。我应该先编译代码,然后再回答^^^尝试使用PathCombine并得到相同的错误,我知道这肯定是一个包含。
std::ofstream
的构造函数不是对
sprintf
的调用
std::ofstream
的构造函数不是对
sprintf
的调用