简体   繁体   中英

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. If r->str_name was "animals" , would it save the file as animals.txt if i concatenate like this?

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:

myfile.open (s.c_str());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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