簡體   English   中英

連接const char * C ++

[英]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();

我正在嘗試連接兩個const char *變量Songtitle和Artistitle。 連接后,我只想使用ofstream寫入文本文件

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

不需要所有這些代碼,也不需要串聯:

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();

請注意,此代碼使用std::string_view不會分配任何額外的空間 ,這是一件好事。 雖然對於幾個短字符串可能並不重要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM