我尝试使用g_io_channel_read_chars方法获取从TCP套接字读取的数据,并将其转换为长整数。我尝试过使用strtol,atoi,而不是将ScanLine转换为gchar指针,使用ScanLine访问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“
发布于 2014-08-21 00:09:07
strtol()接受一个C字符串参数:这意味着字符数组必须以NULL结尾。你的很可能不是。您必须在读取的最后一个字节后添加一个终止符,或者自己解析数字(因为您知道何时停止解析)。
https://stackoverflow.com/questions/25391945
复制相似问题