问题描述
我有,它将分析一些数据的到来。我的问题是用strncpy后,我得到一些垃圾时,我尝试打印功能。我尝试用malloc使字符数组的确切大小。
code:
无效parse_data(字符* unparsed_data)
{
字符* temp_str;
字符* POS;
字符* POS2;
字符*键;
字符*的数据;
为const char换行符='\\ n';INT时间戳= 0;temp_str =(字符*)malloc的(strlen的(unparsed_data));了g_print(\\ n此原始的字符串为:\\ n%S \\ n,unparsed_data);
//忽略前两行
POS =和strchr(unparsed_data,新行);
的strcpy(temp_str,POS + 1);
POS =和strchr(temp_str,新行);
的strcpy(temp_str,POS + 1);//分割线两种;键名和值
POS =和strchr(temp_str,':'); //':'从值除以名
POS2 =和strchr(temp_str,'\\ n'); //队伍的尽头
键=(字符*)malloc的((为size_t)(POS-temp_str)-1); //分配足够的内存
数据=(字符*)malloc的((为size_t)(POS2-POS)-1);函数strncpy(键,temp_str,(为size_t)(POS-temp_str));
函数strncpy(数据,POS + 2,(为size_t)(POS2-POS));时间戳=与atoi(数据);了g_print(键\\的变量\\大小=%d或%d个\\ N(为size_t)(POS-temp_str),strlen的(关键));
了g_print(数据\\变\\的大小=%d或%d个\\ N(为size_t)(POS2-POS),strlen的(数据));了g_print(关键的名字是%S \\ N键);
了g_print(以下简称价值为%s \\ n,数据);
了g_print(分析器\\ n结束);
}
输出:
原始字符串:
NEW_DATAa_PACKET
本地数据集16字节的通用密钥
时间戳(微秒):1319639501097446
帧编号:0
版本:3
角(度):10.228428变量键= 21或22的大小
变量数据= 18或21的大小
关键的名字是时间戳(微秒)
值为1319639501097446
F32
解析器结束
再次运行它:
原始字符串:
NEW_DATAa_PACKET
本地数据集16字节的通用密钥
时间戳(微秒):1319639501097446
帧编号:0
版本:3
角(度):10.228428 变量键= 21或25的大小
变量数据= 18或18的大小
关键的名字是时间戳(微秒)IPE
值为1319639501097446
F
解析器结束
您函数strncpy(数据,POS + 2,(为size_t)(POS2-POS));
没有按't在字符串末尾添加一个终止 \\ 0
字符。因此,当您稍后尝试打印,的printf()
打印你的整个数据串和无论是在之后对它的记忆,直至为零 - 这是garbate你重新获得。您需要使用数据的末尾明确追加为零。它也需要的atoi()
。
编辑:
您需要为您的数据
分配一个多字节,写一个终止符那里。 数据[len_of_data] ='\\ 0'
。只有它成为一个有效的C字符串,你可以使用它的的atoi()后
和的printf()
。
I have a function that will parse some data coming in. My problem is that after using strncpy I get some garbage when I try to print it. I try using malloc to make the char array the exact size.
Code:
void parse_data(char *unparsed_data)
{
char *temp_str;
char *pos;
char *pos2;
char *key;
char *data;
const char newline = '\n';
int timestamp = 0;
temp_str = (char*)malloc(strlen(unparsed_data));
g_print("\nThe original string is: \n%s\n",unparsed_data);
//Ignore the first two lines
pos = strchr(unparsed_data, newline);
strcpy(temp_str, pos+1);
pos = strchr(temp_str, newline);
strcpy(temp_str, pos+1);
//Split the line in two; The key name and the value
pos = strchr(temp_str, ':'); // ':' divides the name from the value
pos2 = strchr(temp_str, '\n'); //end of the line
key = (char*)malloc((size_t)(pos-temp_str)-1); //allocate enough memory
data = (char*)malloc((size_t)(pos2-pos)-1);
strncpy(key, temp_str, (size_t)(pos-temp_str));
strncpy(data, pos + 2, (size_t)(pos2-pos));
timestamp = atoi(data);
g_print("size of the variable \"key\" = %d or %d\n", (size_t)(pos-temp_str), strlen(key));
g_print("size of the variable \"data\" = %d or %d\n", (size_t)(pos2-pos), strlen(data));
g_print("The key name is %s\n",key);
g_print("The value is %s\n",data);
g_print("End of Parser\n");
}
Output:
The original string is:
NEW_DATAa_PACKET
Local Data Set 16-byte Universal Key
Time Stamp (microsec): 1319639501097446
Frame Number: 0
Version: 3
Angle (deg): 10.228428
size of the variable "key" = 21 or 22
size of the variable "data" = 18 or 21
The key name is Time Stamp (microsec)
The value is 1319639501097446
F32
End of Parser
Run it again:
The original string is:
NEW_DATAa_PACKET
Local Data Set 16-byte Universal Key
Time Stamp (microsec): 1319639501097446
Frame Number: 0
Version: 3
Angle (deg): 10.228428
size of the variable "key" = 21 or 25
size of the variable "data" = 18 or 18
The key name is Time Stamp (microsec)ipe
The value is 1319639501097446
F
End of Parser
Your strncpy(data, pos + 2, (size_t)(pos2-pos));
doesn't add a terminating \0
character at the end of the string. Therefore when you try to print it later, printf()
prints your whole data string and whatever is in the memory right after it, until it reaches zero - that's the garbate you're getting. You need to explicitly append zero at the end of your data. It's also needed for atoi()
.
Edit:You need to allocate one more byte for your data
, and write a terminating character there. data[len_of_data] = '\0'
. Only after that it becomes a valid C string and you can use it for atoi()
and printf()
.
这篇关于使用的strcpy当正在打印垃圾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!