#include <iostream>
#include <windows.h>
#include <Lmcons.h>
#include <fstream>
using namespace std;
main(){
char username[UNLEN+1];
DWORD username_len = UNLEN+1;
GetUserName(username, &username_len);
string cmd("C:\\Users\\");
cmd+=username;
cmd+=("\\AppData\\Roaming\\MiniApps");
}
现在我在“cmd”中有完整的路径网址,我想将此变量用作c ++文件处理中的路径。喜欢
ofstream file;
file.open(cmd,ios::out|ios::app);
答案 0 :(得分:1)
使用C ++ 11,你可以做到
ofstream file(cmd,ios::app);
没有你必须做的
ofstream file(cmd.c_str(),ios::app);
答案 1 :(得分:1)
使用ofstream打开文件流,编写内容并关闭。
#include<iostream>
#include <windows.h>
#include <Lmcons.h>
#include <fstream>
#include <string>
int main(){
char username[UNLEN+1];
DWORD username_len = UNLEN+1;
GetUserName(username, &username_len);
std::string cmd("C:\\Users\\");
cmd+=username;
cmd+=("\\AppData\\Roaming\\MiniApps.txt");
std::ofstream file;
file.open (cmd.c_str(), std::ofstream::out | std::ofstream::app);
file << " Hello World";
file.close();
return 0;
}