简体   繁体   English

字符串到字符数组不起作用

[英]String to Char Array not working

I noticed that if my string is empty, the char array does not become empty either. 我注意到,如果我的字符串为空,则char数组也不会为空。 See the below code: 请参见以下代码:

std::copy(strUsername.begin(), strUsername.end(), sRecord.m_strUsername);

sRecord.m_strUsername is a char[1064] , but if it is already populated and i try doing a copy (or strcpy ) with strUsername as empty, nothing happens, even though I want sRecord.m_strUsername to become empty or just become whatever strUsername is. sRecord.m_strUsername是一个char[1064] ,但是如果它已经被填充并且我尝试使用strUsername为空进行copy (或strcpy ),即使我希望sRecord.m_strUsername为空或只是成为任何strUsername都不会发生任何strUsername

You forgot the zero terminator: 您忘记了零终止符:

std::copy(strUsername.begin(), strUsername.end(), sRecord.m_strUsername);
sRecord.m_strUsername[strUsername.size()] = '\0';

or alternatively 或者

std::strcpy(sRecord.m_strUsername, strUsername.c_str() );

or if you want all "unused" characters of your array to be \\0 : 或者如果您希望数组的所有“未使用”字符都为\\0

std::copy(strUsername.begin(), strUsername.end(), sRecord.m_strUsername);
std::fill(sRecord.m_strUsername + strUsername.size(), 
          sRecord.m_strUsername + 1064, '\0');

The latter is usually not necessary since C-style strings are commonly defined as a range like [sRecord.m_strUsername[0], first '\\0' in the array) . 通常不需要后者,因为C样式的字符串通常定义为[sRecord.m_strUsername[0], first '\\0' in the array) What comes after the first \\0 usually does not matter unless you want to access that for whatever reason. 除非您出于任何原因要访问该\\0否则通常无所谓。

The range [str.begin(), str.end()) only contains all the characters of the string. 范围[str.begin(), str.end())仅包含字符串的所有字符。 A C-style string (aka array of char ) requires an additional '\\0' -termination character. C风格的字符串(也称为char数组)需要附加的'\\0'终止字符。

With my first and second proposed fix, the range [sRecord.m_strUsername[0], first '\\0' in the array) will be equal to the characters in strUsername as long as the latter does not contain any additional '\\0' s (something you just cannot easily deal with with C-style strings). 使用我的第一个和第二个建议的修复程序,范围[sRecord.m_strUsername[0], first '\\0' in the array)将等于strUsername的字符,只要后者不包含任何其他的'\\0' (有些事情您无法轻松处理C样式的字符串)。

With my third proposed fixed, every character in the array after all the characters from your string will be \\0 . 固定了我的第三个提议后,数组中所有字符(字符串中所有字符之后的每个字符)都将为\\0

Important note: Before doing any of the above, you need to assert that your string contains at most 1063 characters! 重要说明:执行上述任何操作之前,您需要断言您的字符串最多包含1063个字符! If this is not certain, raise some kind of error if the assumption does not hold or use std::strncopy instead. 如果不确定,如果假设不成立或使用std::strncopy ,则引发某种错误。 strncopy will basically behave like my third snippet, but you have to zero the last element of your array yourself. strncopy基本上会像我的第三个代码片段一样工作,但是您必须自己将数组的最后一个元素清零。

You can use strcpy which automatically copies the terminating null character as well, and std::string::c_str : 您可以使用strcpy来自动复制终止的空字符,以及std::string::c_str

std::strcpy(sRecord.m_strUsername, strUsername.c_str());

So if strUsername is empty ( "" ), m_strUsername will also be equal to "" after that call. 因此,如果strUsername为空( "" ),则该调用之后m_strUsername也将等于""

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

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