0
我想通過網絡發送一個僅包含數值的字符數組,但收到數據後,該數字比以前更長,並帶有一些額外的字符。通過網絡發送更長的C字符數組
比如我送:
579858766387684363076422
strlen size 24
char[] prints correct here
但我得到的是:
Got message: 579858766387684363076422 with length 27
strlen size 27
(also there appear special characters in console at the end)
我發送代碼:
char *aArray = toCharArray(&alpha);
// write to client
n = write(newsockfd, aArray, strlen(aArray));
if (n < 0)
error("ERROR writing to socket");
我收到代碼:
n = read(sockfd, buffer, 512);
if (n < 0)
error("ERROR reading from socket");
printf("Got message: %s with length %d\n", buffer, n);
有誰知道最近怎麼回事?順便說一下,我是C的初學者。
我這是怎麼生成的數字陣列我的字符數組,如果有人想看一看:
char * toCharArray(largenum *num) {
char *output = malloc(sizeof(char) * num->size);
for (int i = 0 ; i < num->size; ++i)
{
output[i] = num->digits[i] + '0';
}
return output;
}
你可以爲終止字符'\ 0'做些什麼嗎? 'char * output = malloc(sizeof(char)*(num-> size + 1));'和'output [num-> size] ='\ 0'' – francis 2014-11-23 16:35:43
嘿@francis:這給了我server.c: 169:23:警告:賦值使指針中的整數沒有轉換[缺省情況下啓用]輸出[num-> size] =「\ 0」; 我該如何解決這個問題? :) – luuksen 2014-11-23 16:45:23
@luuksen:仔細閱讀。單引號,而不是雙引號。 – 2014-11-23 16:51:00