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

发布于 2024-11-17 12:16:41 字数 203 浏览 5 评论 0原文

我的项目有问题。我的桌面上有一个项目文件夹。我想创建一个文本文件并写入包含该文本文件的内容。这是我的代码:

ofstream example("/Users/sample/Desktop/save.txt");

但我希望它可以在其他 Mac 上运行。我不知道应该为 save.txt 写什么地址。

有人可以帮助我吗?

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. I don't know what I should write addres for save.txt.

Can anyone help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽容 2024-11-24 12:16:41

创建一个文件并向其写入一些文本很简单,这里是一个示例代码:

   #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;
   }

我希望能回答您的问题,但我不确定我是否正确理解您的问题,如果没有,请正确添加您想要实现的详细信息。

[更新]:
好吧,我想评论解决了问题。
您真正的问题是,您想要将文件保存在正在玩游戏的用户的桌面上。因此,获取当前用户桌面的路径是问题所在。

我不确定是否有一种可移植的方法来获取桌面路径,但可以通过以下方式完成:

在 Windows 中:
使用SHGetSpecialFolderPath()< /strong> 功能。

示例代码:

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

在 Linux 中:
通过使用环境变量 $HOME

示例代码:

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

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.

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

sample code:

string path(getenv("HOME"));
path += "/Desktop/save.txt";
ofstream o(path);
心的憧憬 2024-11-24 12:16:41

定义文件保存位置的规则因平台而异。一种选择是将其作为编译脚本的一部分(即 #define SAVEGAME_PATH 作为编译配置的一部分),因此您的代码本身仍然与平台无关。

另一种方法是找到一个已设计为可以跨不同平台移植的保存数据管理库。无论它是 C 还是 C++ 或者任何二进制可互操作的库都不再重要了。

只是不要指望它会成为 C++(语言)的一部分。

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.

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.

Just don't expect that to be part of C++ (the language).

无语# 2024-11-24 12:16:41

如果你想让你的程序跨平台运行,你最好使用
相对路径。
例如。 “./output.txt”,或者更好的“GetSystemDirectory()”来获取系统
目录创建文件,然后可以写入或读取该文件
具有相同的路径..

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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文