2012-07-24 109 views
0

嗨,我有以下代码:C++字符串到固定大小的字符数组可能吗?

char msg[10000]; 
string mystr = "hello"; 

我想把myStr中成味精。有没有办法做到这一点?我试过各种方法,但不断收到:

incompatible types in assignment of 'const char*' to char [10000]' 

我想:

msg = mystr.c_str(); 

msg = (char[10000])mystr; 

无济于事。

+0

使用strcpy函数 – 2012-07-24 07:17:18

+1

可能重复[如何在C++中将字符串转换为char *](http://stackoverflow.com/questions/9309961/how-to-convert-string-to-char-in-c ) – RedX 2012-07-24 07:20:08

+1

可能的重复[如何将char *复制到字符串中,反之亦然](http://stackoverflow.com/questions/2564052/how-to-copy-char-into-a-string-and-vice-相反) – jogojapan 2012-07-24 07:21:34

回答

4

您可以尝试std::copy这一点。喜欢的东西:

std::copy(mystr.begin(), mystr.end(), msg); 

我会避免像mempcystrcpy在C++ C字符串函数。

+1

据推测,msg应该是零终止的。 – 2012-07-24 07:23:18

+0

@MikeSeymour是的,如果'msg'必须是一个合适的'cstring',则需要第二步。 – cnicutar 2012-07-24 07:24:32

+1

如果一步完成这项工作,为什么要避免使用strcpy? – 2012-07-24 07:30:24

0

C中的字符串赋值是不同的。您必须将字节复制到目标字符串中。

memcpy_s(msg, 1000, mystr.c_str(), mystr.length()) // safe windows version

memcpy(msg, mystr.c_str(), mystr.length()) // unix version

+0

如果消息长度超过10000个字符,这会导致缓冲区溢出,对吧?它应该可能是mystr.length()> 10000? 10000:最后一个参数的mystr.length()或类似的东西。 – Sarien 2012-07-24 07:19:07

+0

@CorporalTouchy是的。 – RedX 2012-07-24 07:20:42

2

看看string::copy - 它需要一个字符串把它放入一个数组中。

你的情况,那就是:

std::size_t length = mystr.copy(msg,10000); 
msg[length]='\0'; 
+1

但请注意,它不会零终止结果;如果你需要的话,你必须自己动手。 – 2012-07-24 07:26:10

0

使用strcpy函数:的std :: string的 http://www.cplusplus.com/reference/clibrary/cstring/strncpy/

strncpy(msg, mystr.c_str(), sizeof msg/sizeof msg[0]); 
msg[sizeof msg/sizeof msg[0] - 1] = 0; // null-terminate in case of truncation 
+3

或'strncpy',如果你不喜欢缓冲区溢出。 – 2012-07-24 07:20:38

+0

肯定strncpy会更好。 – 2012-07-24 07:21:45

+1

要防止缓冲区溢出,您需要将目标的最大大小传递给strncpy,而不是源的大小。 – 2012-07-24 07:26:19

1

使用复印成员函数:

size_t len = mystr.copy(msg, (sizeof msg)-1); 
msg[len] = 0; 
+2

'copy(msg,(sizeof msg)-1)'会避免缓冲区溢出。 – 2012-07-24 07:25:15

1
char msg[10000]; 
string mystr = "hello"; 

strcpy(msg, mystr.c_str()); 
cout<<msg; 
0

编译器有时会为数组类型产生不可靠的错误消息。

下面是以前的答案到一个粘贴和编译程序的积累。

#include <string> 
#include <iostream> 

#if 1 

int main(int argc, char **argv) 
{ 
    using std::cout; 
    using std::endl; 

    char msg[1000] = {0}; // initialize to 0 here since we're printing below 
          // the <type> <array-name>[<size>] = {0} just fills a POD struct or an array with 0s 

    std::string mystr = "hello"; 

    // if, at some point, you have things changing "mystr" 
    // you'll need to make sure that it will fit in msg[] 

    cout << "Before strcpy: \"" << msg << "\"" << endl; 

    // I'll just finish the statement in mystr... 
    mystr += " world!"; 

    if(mystr.length() < sizeof(msg)){ 
     strcpy(
      msg,   // <- put in here until we find a '\0' 
      mystr.c_str() // <- take from here (which could be a temporary buffer) 
      ); 
    } 

    //MSC will complain about strcpy being unsafe 
    // 
    // you can use the below instead (if you really feel the need to), which is 
    // the MS-specific equivalent to the above. 
    /* 
     strcpy_s(
      msg,   // <- put in here until we find a '\0' or the size limit is reached 
      sizeof(msg), // <- don't put any more than this many chars in msg 
      mystr.c_str() // <- take from here 
      ); 
    */ 

    cout << "After strcpy: \"" << msg << "\"" << endl; 

    return 0; 
} 

#else 

// Similarly, using wchar_t (a usually non-byte-sized character type) 
// 
// note where the divisions occurr 

int main(int argc, char **argv) 
{ 
    using std::wcout; 
    using std::endl; 

    wchar_t msg[1000] = {0}; 
    std::wstring mystr = L"hello"; 

    wcout << "Before strcpy: \"" << msg << "\"" << endl; 

    mystr += L" world"; 

    if(mystr.length() < (sizeof(msg)/sizeof(wchar_t))){ 
     // mystr wil fit! 
     wcscpy(
      msg,   // <- put in here until we find a '\0' 
      mystr.c_str() // <- take from here (which could be a temporary buffer) 
      ); 
    } 

    // Similar to the char case in the first preprocessor block 
    /* 
     wcscpy_s(
      msg,       // <- put in here until we find a '\0' or the size limit is reached 
      sizeof(msg)/sizeof(wchar_t), // <- don't put any more than this many wchar_ts in msg 
      mystr.c_str()     // <- take from here 
      ); 
    */ 

    wcout << "After strcpy: \"" << msg << "\"" << endl; 

    return 0; 
} 

#endif 

我将留给您阅读所有相关功能的文档。

相关问题