0

我正在尝试使用 g_io_channel_read_chars 方法从 TCP 套接字读取数据并将其转换为长整数。我尝试使用 strtol、atoi,不将 ScanLine 转换为 gchar 指针,使用 ScanLine[0] 访问 ScanLine 的第一个变量,以不同的方式声明 FilterAmount,尽管如此,我的应用程序仍然在该行崩溃。有任何想法吗?

static gchar ScanLine[9640];
long int FilterAmount;

g_io_channel_read_chars (source, (gchar *) ScanLine,1,&BytesRead,&GlibError);
if (BytesRead != 1){ 
    return TRUE;
} 

printf("This is my string: %s\n", ScanLine);
FilterAmount = strtol(ScanLine, NULL, 10); 

printf 语句的输出为“2”

4

1 回答 1

0

strtol() 采用 C 字符串参数:这意味着字符数组必须以 NULL 结尾。你的很可能不是。您必须在读取的最后一个字节之后添加终止符,或者自己解析数字(因为您知道何时停止解析)。

于 2014-08-20T16:09:07.323 回答