簡體   English   中英

將char []附加到std :: string並加上特定長度

[英]append char[] to std::string with a spcific lenght

我想從串行端口連接中接收完整的字符串。 接收函數給我一個char[]和讀取字符的值。 所以我想將所有已讀取的字符添加到std::string直到\\r\\n字符為止。 這是我的代碼,例外是在append處:

    //----- CHECK FOR ANY RX BYTES -----
    if (uart0_filestream != -1)
    {
        // Read up to 255 characters from the port if they are there
        unsigned char rx_buffer[256];
        int rx_length = read(uart0_filestream, (void*)rx_buffer, 255);      //Filestream, buffer to store in, number of bytes to read (max)
        if(rx_length > 0){
            std::string result = "";
        //  res = "";
            while (rx_length > 0)
            {
                result.append(*rx_buffer, 0, rx_length);
                rx_length = read(uart0_filestream, (void*)rx_buffer, 255);
            }
            std::cout << "rec" << result << std::endl;
        }

    }

有誰知道如何解決這個問題?

改變這個

result.append(*rx_buffer, 0, rx_length);

對此

result.append(rx_buffer, 0, rx_length);

並確保CPP源文件中包含#include <string>

暫無
暫無

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

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