C++文件读取中:
infile in;
in.open("file.dat",ios::in);
这样是能够的。
可是
string a;
a="file.dat"
in.open(a,ios::in)
这种格式编译器将要报错,原因是C++不能识别字符串的文件名称。
有时候须要自己定义的文件名称。就要把string类型转换成char型的。
比方:
string a="hello world";
char *b=new char[20];
strcpy(b,a.c_str());
cout<<b<<endl;
cout<<a.c_str()<<endl;
delete b;
结果将例如以下显示:
这样
in.open(b,ios::in);
就能够编译通过了。
@ Mayuko