C++ 如何在桌面文件夹中创建文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6477568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a text file in a folder on the desktop
提问by muhammedkasva
I have a problem in my project. There is a project folder on my desktop. I want to create a text file and write something include this text file. That is my code:
我的项目有问题。我的桌面上有一个项目文件夹。我想创建一个文本文件并编写一些包含此文本文件的内容。那是我的代码:
ofstream example("/Users/sample/Desktop/save.txt");
ofstream example("/Users/sample/Desktop/save.txt");
But I want to it could been run the other mac. I don't know what I should write addres for save.txt.
但我希望它可以在另一台 mac 上运行。我不知道我应该为 save.txt 写什么地址。
Can anyone help me?
谁能帮我?
回答by Alok Save
Create a file and write some text to it is simple, here is a sample code:
创建一个文件并向其中写入一些文本很简单,这是一个示例代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
std::ofstream o("/Users/sample/Desktop/save.txt");
o << "Hello, World\n" << std::endl;
return 0;
}
I hope that answers your question but I am not sure if i understand your question correctly, If not please add the details correctly of what you are trying to acheive.
我希望能回答您的问题,但我不确定我是否正确理解您的问题,如果不是,请正确添加您想要实现的详细信息。
[Update]:
Okay I guess the comment clears the problem.
Your real question is, You want to save the file in the desktop of the user who is playing the game. So getting the path of the current user's desktop is the problem.
[更新]:好的,我想评论解决了问题。
您真正的问题是,您想将文件保存在正在玩游戏的用户的桌面中。所以获取当前用户桌面的路径是个问题。
I am not sure if there is an portable way to get desktop path but it can be done in following ways:
我不确定是否有一种可移植的方式来获取桌面路径,但可以通过以下方式完成:
In Windows:
Using the SHGetSpecialFolderPath()function.
在 Windows 中:
使用SHGetSpecialFolderPath()函数。
Sample code:
示例代码:
char saveLocation[MAX_PATH] = {0};
SHGetSpecialFolderPath(NULL, saveLocation, CSIDL_DESKTOPDIRECTORY, FALSE);
//Now saveLocation contains the path to the desktop
//Append your file name to it
strcat(saveLocation,"\save.txt");
ofstream o(saveLocation);
In Linux:
By using environment variables $HOME
在 Linux 中:
通过使用环境变量 $HOME
sample code:
示例代码:
string path(getenv("HOME"));
path += "/Desktop/save.txt";
ofstream o(path);
回答by PypeBros
Rules defining where-you-should-save-file vary from platform to platform. One option would be to have it part of your compile script (that is you #define SAVEGAME_PATH as part of your compilation configuration), and thus your code itself remain more platform-agnostic.
定义文件保存位置的规则因平台而异。一种选择是将它作为编译脚本的一部分(也就是说,您将 #define SAVEGAME_PATH 作为编译配置的一部分),因此您的代码本身仍然与平台无关。
The alternative is to find a save-data-management library that is already designed to be ported across different platforms. Whether it'd be a C or C++ or whatever-binary-interoperable library then no longer matters.
另一种方法是找到一个已经设计为可以跨不同平台移植的保存数据管理库。无论是 C 或 C++ 还是任何二进制互操作的库,都不再重要。
Just don't expect that to be part of C++ (the language).
只是不要期望它成为 C++(语言)的一部分。
回答by shenju
if you want your program to run across platform,you'd better use the relative path. eg. "./output.txt",or better “GetSystemDirectory()”to obtain the system directory to create a file,and then you could write or read the file with the same path..
如果你想让你的程序跨平台运行,你最好使用相对路径。例如。“./output.txt”,或者更好的“GetSystemDirectory()”获取系统目录创建文件,然后可以写入或读取相同路径的文件。