C++中open() 函数不能传string的解决方法

来源:互联网 发布:淘宝开店开通不了 编辑:程序博客网 时间:2024/05/22 05:38

C++ 的open()只能传入const char * 类型的,当传入string类型的就会出错,对此C++中提供了c_str()的解决方法

例如:

        ofstream out;

       string filename;

       filename = "C:\\Users\\Administrator\\0113\\01\\" + argv[1];

       out.open(filename.c_str());

       out << "hello world!" << endl;

       out.close();

就可以在制定目录下生成 以argv[1]命名的文件了;

但是要对 filename.c_str();修改就只能用c的 strcpy()等函数了。


0 0