使用std :: string打开文件

Dan*_*Dan 2 c++ file-io stl

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

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

ofstream output;

output.open(path);
Run Code Online (Sandbox Code Playgroud)

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

Fre*_*ool 9

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

您也可以在字符串上使用'c_str()'方法来获取底层C字符串.

output.open(path.c_str());
Run Code Online (Sandbox Code Playgroud)