C++中文件名称必须是C风格的char*格式

File Names in C++
For historical reasons, the IO library uses C-style character strings (Section
4.3 , p. 130 ) rather than C++ strings to refer to file names. When we call
open or use a file name as the initializer when creating an fstream object, the
argument we pass is a C-style string, not a library string . Often our
programs obtain file names by reading the standard input. As usual, it is a
good idea to read into a string , not a C-style character array. Assuming
that the name of the file we wish to use is in a string , we can use the c_str
member (Section 4.3.2 , p. 139 ) to obtain a C-style string.

 

在C++ Primer一书中明确说明了,C++中读入文件操作(fstream类),中的文件名称必须是C_Style名称,

而是用C++ 时使用更多的是 library string , 利用string读入,调用函数c_str转换成char* (C_style)类型的文件

 

在OpenCV中的cvLoadImage参数也是char* 类型.

OpenCV中的imread 函数参数则是string类型的.

使用 c_str() 函数转格式即可.

原文地址:https://www.cnblogs.com/xiangwengao/p/2311790.html