簡體   English   中英

C ++字符串到固定大小的char數組可能嗎?

[英]C++ string to fixed sized char array possible?

嗨,我有以下代碼:

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;

無濟於事。

您可以嘗試使用std::copy 就像是:

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

我會避免在C ++中使用像mempcystrcpy這樣的C字符串函數。

看一下string :: copy-它需要一個字符串並將其放入數組中。

您的情況是:

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

使用std :: string的復制成員函數:

size_t len = mystr.copy(msg, (sizeof msg)-1);
msg[len] = 0;
char msg[10000];
string mystr = "hello";

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

C中的字符串分配不同。 您必須將字節復制到目標字符串中。

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

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

使用strcpy函數: 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

編譯器有時會產生數組類型的錯誤消息。

這是粘貼和編譯程序中先前答案的積累。

#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

我將留給您閱讀有關所有相關功能的文檔。

暫無
暫無

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

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