用std :: string打开文件 [英] Opening a file with std::string

查看:67
本文介绍了用std :: string打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是一个相当琐碎的问题.我正在尝试使用std :: string(或std :: wstring)打开ofstream,并且在不进行麻烦的转换的情况下无法正常工作.

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

理想情况下,如果有更好的方法,我不想手动转换它或使用c样式的char指针?

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?

推荐答案

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

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

另外,您可以在字符串上使用'c_str()'方法来获取基础的C字符串.

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

output.open(path.c_str());

这篇关于用std :: string打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆