使用 std::string 打开文件
这应该是一个相当微不足道的问题。 我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在路径字符串中,使用两个点而不是三个。
您还可以在字符串上使用“c_str()”方法来获取底层 C 字符串。
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())
this should work:
output.open(path.c_str())
恐怕根本不可能。 你必须使用 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.