如何让fstream保存到任何在c++中打开.exe的windows桌面



我正在为我哥哥制作一个程序,该程序将显示50000个代理变体,并将它们全部保存到.txt.中

我如何才能让任何使用此代码的windows机器都能将.txt文件保存到桌面上。

这是我的:

fstream file;
file.open("proxies.txt", ios::out);
string line;
streambuf* stream_buffer_cout = cout.rdbuf();
streambuf* stream_buffer_cin = cin.rdbuf();
streambuf* stream_buffer_file = file.rdbuf();
cout.rdbuf(stream_buffer_file);

for (int i = 1; i < 50001; i++)
{
cout << n1 << i << n2 << "n";
}
file.close();

谢谢你的帮助。

如果我得到了你的要求,你只需要替换"proxys.txt";带有指向桌面文件夹中文件的绝对路径。您可以使用Win32调用SHGetFolderPath获取桌面目录,并根据需要使用标准(C++17(文件系统调用将路径放在一起,如下所示:

#include <iostream>
#include <filesystem>
#include <fstream>
#include <shlobj_core.h>
namespace fs = std::filesystem;
std::string desktop_directory() {
char path[MAX_PATH + 1];
if (SHGetFolderPathA(HWND_DESKTOP, CSIDL_DESKTOP, NULL, 
SHGFP_TYPE_DEFAULT, path) == S_OK) {
return path;
} else {
return {}; // I'm not sure why this would fail...
}
}
int main() {
std::fstream file;
auto desktop_path = fs::path(desktop_directory()) / "proxies.txt";
file.open(desktop_path, std::ios::out);
// ...
file.close();
return 0;
}

最新更新