使用 std::string 打开文件

发布于 2024-07-13 12:21:52 字数 250 浏览 6 评论 0原文

这应该是一个相当微不足道的问题。 我正在尝试使用 std::string (或 std::wstring)打开一个 ofstream,并且在没有混乱转换的情况下使其正常工作时遇到问题。

std::string path = ".../file.txt";

ofstream output;

output.open(path);

理想情况下,如果有更好的方法的话,我不想手动转换它或涉及 c 风格的字符指针?

This should be a fairly trivial problem. I'm trying to open an ofstream using a std::string (or std::wstring) and having problems getting this to work without a messy conversion.

std::string path = ".../file.txt";

ofstream output;

output.open(path);

Ideally I don't want to have to convert this by hand or involve c-style char pointers if there's a nicer way of doing this?

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

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

发布评论

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

评论(3

微凉徒眸意 2024-07-20 12:21:52

在路径字符串中,使用两个点而不是三个。

您还可以在字符串上使用“c_str()”方法来获取底层 C 字符串。

output.open(path.c_str());

In the path string, use two dots instead of three.

Also you may use 'c_str()' method on string to get the underlying C string.

output.open(path.c_str());
若沐 2024-07-20 12:21:52

这应该有效:

output.open(path.c_str())

this should work:

output.open(path.c_str())

夜还是长夜 2024-07-20 12:21:52

恐怕根本不可能。 你必须使用 c_str,是的,它很糟糕。

顺便说一句,使用 char* 也意味着 fstream 不支持 Unicode 文件名……真可惜。

I'm afraid it's simply not possible. You have to use c_str, and yes, it sucks.

Incidentally, using char* also means fstream has no support for Unicode file names... a shame.

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