简体   繁体   中英

Concatenate const char* C++

basic_string<TCHAR> titleChar( szTitle );
string titleStr( titleChar.begin(), titleChar.end() );
const char* Songtitle = titleStr.c_str();

basic_string<TCHAR> artisTChar( szArtist );
string artitstStr( artisTChar.begin(), artisTChar.end() );
const char* Artistitle= artitstStr.c_str();

I'm trying to concatenate two const char* variables Songtitle & Artistitle. After concatenating i want simply write in a text file using ofstream

ofstream file;
file.open("D:\\lrc\\lyricsub\\songname.txt");
file << Songtitle;
file.close();

No need for all that code, nor for the concatenation:

std::string_view title { szTitle, strlen(szTitle) };
std::string_view artist_name { szArtist, strlen(szArtist) };
ofstream file;
file.open("D:\\lrc\\lyricsub\\songname.txt");
file << title << ' ' << artist_name;
file.close();

Note that this code, which uses std::string_view 's, will not allocate any extra space , which is a Good Thing. Although it might not really matter for a couple of short strings.

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