使用 strcpy 时打印的垃圾 [英] Garbage being printed when using strcpy

查看:24
本文介绍了使用 strcpy 时打印的垃圾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数可以解析一些传入的数据.我的问题是,在使用 strncpy 后,当我尝试打印它时,我得到了一些垃圾.我尝试使用 malloc 使 char 数组具有精确的大小.

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.

代码:

void parse_data(char *unparsed_data)
{
char *temp_str;
char *pos;
char *pos2;
char *key;
char *data;
const char newline = '
';

int timestamp = 0;

temp_str = (char*)malloc(strlen(unparsed_data));

g_print("
The original string is: 
%s
",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, '
'); //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
", (size_t)(pos-temp_str), strlen(key));
g_print("size of the variable "data" = %d or %d
", (size_t)(pos2-pos), strlen(data));

g_print("The key name is %s
",key);
g_print("The value is %s
",data);
g_print("End of Parser
");
  }

输出:

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

再次运行:

  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

推荐答案

你的 strncpy(data, pos + 2, (size_t)(pos2-pos)); 没有添加终止 字符在字符串的末尾.因此,当您稍后尝试打印它时, printf() 会打印您的整个数据字符串以及紧随其后的内存中的任何内容,直到它达到零 - 这就是您得到的垃圾.您需要在数据末尾明确附加零.atoi() 也需要它.

Your strncpy(data, pos + 2, (size_t)(pos2-pos)); doesn't add a terminating 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().

您需要为 data 再分配一个字节,并在那里写入一个终止字符.data[len_of_data] = ''.只有在此之后它才成为有效的 C 字符串,并且您可以将其用于 atoi()printf().

You need to allocate one more byte for your data, and write a terminating character there. data[len_of_data] = ''. Only after that it becomes a valid C string and you can use it for atoi() and printf().

这篇关于使用 strcpy 时打印的垃圾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆