简体   繁体   English

将字符串复制到char数组

[英]Copy a string to char array

I am trying to copy a string which might contain null characters in the middle to a char array. 我正在尝试将可能在中间包含空字符的字符串复制到char数组。 I have constructed the following function. 我构造了以下功能。

 void SaveStringToChar(string &mystring,const char * &ArrChar)
 {//begin function

   std::string str;
   char * writable = new char[str.size() + 1];
   std::copy(str.begin(), str.end(), writable);
   writable[str.size()] = '\0';
   ArrChar = writable;

 }//end function

my question is this method guarantees that I wont lose the characters after a null element. 我的问题是,此方法可确保我不会在null元素后丢失字符。 and my other question is I get this linker error which I don't know what it means. 我的另一个问题是我收到此链接器错误,但我不知道这意味着什么。

/tmp/ccUpCRaz.o: In function `Parser::RuleParser(char const*)': Parser.cpp:(.text+0x3f6): undefined reference to Parser::SaveStringToChar(std::basic_string, std::allocator >&, char const*&)' collect2: ld returned 1 exit status /tmp/ccUpCRaz.o:在函数`Parser :: RuleParser(char const *)'中:Parser.cpp :(。text + 0x3f6):对Parser :: SaveStringToChar(std :: basic_string,std :: allocator>的未定义引用> &,char const *&)'collect2:ld返回1退出状态

anyhint please. 请任何提示。

this is the function I pass things to compare. 这是我通过比较的功能。

  void Search( size_t TextLength, const char *Text, const vector<const char *> &patterns );

You need to define the function as a member of Parser:: . 您需要将函数定义为Parser::的成员。 It is currently defined as: 当前定义为:

void SaveStringToChar(string &mystring,const char * &ArrChar)

Change to: 改成:

void Parser::SaveStringToChar(string &mystring,const char * &ArrChar)

The std::copy() will copy all the characters. std::copy()将复制所有字符。 However, the caller will not know how many characters ArrChar actually points to as functions like strlen() will stop counting at the first null character. 但是,调用者将不知道ArrChar实际指向多少个字符,因为诸如strlen()类的函数将在第一个空字符处停止计数。 You could change the signature of SaveStringToChar() to accept another argument size_t& ArrCharLen that would be populated with the number of characters in ArrChar . 您可以更改SaveStringToChar()的签名以接受另一个参数size_t& ArrCharLen ,该参数将填充size_t& ArrCharLen中的字符ArrChar Of course, mystring will have the length of ArrChar so maybe you are already storing this prior to the call to SaveStringToChar() . 当然, mystring具有ArrChar的长度,因此也许您已经在调用SaveStringToChar()之前存储了它。

EDIT (after vector<const char*> comment): 编辑(在vector<const char*>注释之后):

The true number of bytes is lost once the const char* is added to the vector . 一旦将const char*添加到vector ,字节的真实数量就会丢失。 Why not use a vector<std::string> and you have std::string::length() to know how many characters there are actually stored in std::string . 为什么不使用vector<std::string> ,却拥有std::string::length()来知道std::string中实际存储了多少个字符。

EDIT2 (after comparison function .c_str() with text.length(): EDIT2(在具有text.length()的比较函数.c_str()之后:

Instead of passing .c_str() and .length() to compare against elements in a vector<const char*> just pass the std::string to the compare function and compare against elements of a vector<std::string> : the std::string::operator==() will correctly compare strings will embedded null characters. 不用传递.c_str().length()vector<const char*>中的元素进行比较,只需将std::string传递给compare函数并与vector<std::string>元素进行比较: std::string::operator==()将正确比较字符串,并将嵌入空字符。 You could then use std::find() to search the vector<std::string> for the std::string read from the file. 然后,您可以使用std::find()vector<std::string>搜索从文件读取的std::string

Your string copy seem fine, but if you are doing it to pass a char* to a function there are better ways see: 您的字符串副本看起来不错,但是如果您执行此操作以将char *传递给函数,则有更好的方法请参见:

std::string str;
//...
my_func(str.c_str());

hmjd has your link error. hmjd出现链接错误。

See hmjd's answer for the link error. 有关链接错误,请参见hmjd的答案。

As to the copied characters, you won't actually lose any characters, because they're all safely copied to the output buffer. 至于复制的字符,实际上不会丢失任何字符,因为它们都已安全地复制到了输出缓冲区中。 However, if there are really \\0 characters in the middle of the string, none of the C-style string functions (like printf) will work correctly on it. 但是,如果字符串中间确实有\\0字符,则C风格的字符串函数(例如printf)都不能在其上正常工作。
And also the calling routine won't know what the length of the string was, and therefore what the newly allocated size of the buffer is. 而且,调用例程将不知道字符串的长度,因此也不知道缓冲区的新分配大小。 You should return the string length too (either as the return value, or in another reference argument). 您也应该返回字符串长度(作为返回值,或在另一个引用参数中)。

my question is this method guarantees that I wont lose the characters after a null element 我的问题是这种方法保证了我不会在null元素后丢失字符

The whole string will be copied into the buffer, even if it does contain null characters. 整个字符串将被复制到缓冲区中,即使它确实包含空字符也是如此。 std::copy does not interpret the data it copies in any way, it just copies the number of elements you tell it to. std::copy不会以任何方式解释它复制的数据,它只是复制您告诉它的元素数量。

However, if the string does contain null characters, then you can't use it with any function that works with null-terminated strings (such as those in <cstring> ); 但是,如果字符串确实包含空字符,则不能将其与任何以空字符结尾的字符串一起使用的函数(例如<cstring>中的<cstring> )一起使用; they will interpret the first null character as the end of the string. 他们会将第一个空字符解释为字符串的结尾。

Also, at least in the code you posted, you are returning the pointer as const char * , implying that you won't be modifying the copy. 另外,至少在您发布的代码中,您将以const char *形式返回指针,这意味着您不会修改副本。 In that case, do you actually need a copy of the string's data, or is it enough to return str.c_str() instead? 在这种情况下,您实际上是否需要字符串数据的副本,还是足以返回str.c_str()

my other question is I get this linker error 我的另一个问题是我得到此链接器错误

It looks like you're missing the definition of Parser::SaveStringToChar . 似乎您缺少Parser::SaveStringToChar的定义。 I'm guessing that the code you've posted is from a source file, and not a function defined inline inside the Parser class. 我猜您发布的代码来自源文件,而不是Parser类中内联定义的函数。 If that's the case, then you've forgotten to specify that it's a member function, and defined a non-member instead; 如果是这样,那么您就忘记了指定它是成员函数,而是定义了一个非成员函数; change it to 更改为

void Parser::SaveStringToChar(string &mystring,const char * &ArrChar)
     ^^^^^^^^

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

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