简体   繁体   English

正确保存到带有文件名的文件

[英]Properly Saving to file with a file name

 ofstream myfile;
    string s=r->str_name+".txt";
    myfile.open (s);

where r->str_name is a string. 其中r-> str_name是字符串。 If r->str_name was "animals" , would it save the file as animals.txt if i concatenate like this? 如果r-> str_name是“ animals”,如果我将其连接起来,是否会将文件另存为animals.txt?

Close. 关。 It does do as you expect in that r->str_name will be "animals.txt" but to pass it to myfile.open() you have to turn it into a const char* like so: 它确实如您期望的那样在r->str_name将是“ r->str_name ”,但要将其传递到myfile.open()您必须将其转换为const char*如下所示:

myfile.open (s.c_str());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM