C++ 连接常量字符*C++; basic_字符串标题(szTitle); 字符串titleStr(titleChar.begin(),titleChar.end()); const char*songtile=titleStr.c_str(); 基本字符串artisTChar(szArtist); 字符串artitstr(artisTChar.begin(),artisTChar.end()); 常量char*Artistitle=artitstr.c_str();

C++ 连接常量字符*C++; basic_字符串标题(szTitle); 字符串titleStr(titleChar.begin(),titleChar.end()); const char*songtile=titleStr.c_str(); 基本字符串artisTChar(szArtist); 字符串artitstr(artisTChar.begin(),artisTChar.end()); 常量char*Artistitle=artitstr.c_str();,c++,visual-studio,char,constants,ofstream,C++,Visual Studio,Char,Constants,Ofstream,我试图连接两个常量char*变量songtile和Artistitle。在连接之后,我只想使用ofstream在文本文件中写入 basic_string<TCHAR> titleChar( szTitle ); string titleStr( titleChar.begin(), titleChar.end() ); const char* Songtitle = titleStr.c_str(); basic_string<TCHAR> artisTChar( sz

我试图连接两个常量char*变量songtile和Artistitle。在连接之后,我只想使用ofstream在文本文件中写入

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();
流文件的
;
打开(“D:\\lrc\\lyricsub\\songname.txt”);

文件不需要所有代码,也不需要连接:

ofstream file;
file.open("D:\\lrc\\lyricsub\\songname.txt");
file << Songtitle;
file.close();
std::string_视图标题{szTitle,strlen(szTitle)};
std::string_view artist_name{szArtist,strlen(szArtist)};
流文件;
打开(“D:\\lrc\\lyricsub\\songname.txt”);

文件将它们连接为字符串调用c_str(),并使用
文件从基于
TCHAR
的字符串转换为窄字符串将产生基本上随机的字符,如果在
char
中存在一些无法容纳的内容,并且您正在使用定义的
UNICODE
进行构建(应该在现代Windows应用程序中)。大多数字符都不能放入
char
@Jarod42谢谢你,这很容易操作tanks@einpolum使用strcpy怎么样<代码>字符concatSname[200];strcpy(concatSname,Artsttitle);strcat(concatSname,“”);strcat(concatSname,歌曲名称)这个问题解决了,但当我尝试实现你的方法时,字符串视图并没有解决working@Nik: 1. 底线:绝对不是,你根本不需要复制任何东西。2.200是任意大小。3.你不能假设你的堆栈足够大,可以容纳一个字符串,而不能预先绑定它的大小。3.你的意思是
strncpy()
。4.这段代码将遍历字符串多次,这是一种浪费。5.为什么在C++中编写C代码?6.当您真的需要在缓冲区中连接字符串时,您可能会使用
std::stringstream
。谢谢,我找到了它。我只是简单地连接了
文件@enipoklum,如果你投票支持我的问题,那就太好了,谢谢
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();