简体   繁体   English

如何在桌面上的文件夹中创建文本文件

[英]How to create a text file in a folder on the desktop

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");

But I want to it could been run the other mac. 但我希望它可以运行其他的mac。 I don't know what I should write addres for save.txt. 我不知道我应该为save.txt写些什么。

Can anyone help me? 谁能帮我?

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: 在Windows中:
Using the SHGetSpecialFolderPath() function. 使用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: 在Linux中:
By using environment variables $HOME 通过使用环境变量$ HOME

sample code: 示例代码:

string path(getenv("HOME"));
path += "/Desktop/save.txt";
ofstream o(path);

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 ++(语言)的一部分。

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()”获取系统目录以创建文件,然后您可以使用相同的路径写入或读取文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM