在struct中将字符数组划分为不同的值
通过tcp套接字,我得到如下字符串:“SmallTitle fromHost9595” 收到后,我想将其划分为一个结构,以便更容易使用。 这就是我想到的。但也存在一些问题。真讨厌。2当在末尾打印结构时,我在字符串之后接收垃圾输出。请参见底部的输出示例。printf不应该在“\0”停止吗?那么它可能会错过这样的机会在struct中将字符数组划分为不同的值,c,C,通过tcp套接字,我得到如下字符串:“SmallTitle fromHost9595” 收到后,我想将其划分为一个结构,以便更容易使用。 这就是我想到的。但也存在一些问题。真讨厌。2当在末尾打印结构时,我在字符串之后接收垃圾输出。请参见底部的输出示例。printf不应该在“\0”停止吗?那么它可能会错过这样的机会 struct recieved { char what[50]; char from[50]; int value; }; int main(int arg
struct recieved {
char what[50];
char from[50];
int value;
};
int main(int argc, char const *argv[]) {
struct recieved leaderRec;
char *word;
char buf[] = "leadervalue host 9569"; //Example recieved input from socket.
word = (char *) malloc(sizeof(char) * 50);
int i = 0;
int count = 1;
do{
if((buf[i] == ' ') || (buf[i+1] == '\0')){
//End of word
//word[strlend(word)+1] = '\0';
//printf("%s\n", word);
if(count == 3){
//The value is here
//get the last number
strncat(word,&buf[i], 1);
leaderRec.value = atoi(word);
//printf("%d\n", value);
}
if(count == 1)
strncpy(leaderRec.what, word, strlen(word));
else if(count == 2)
strncpy(leaderRec.from, word, strlen(word));
free(word);
word = (char *) malloc(sizeof(char) * 50);
count++;
}
else {
//printf("%d\n",i );
//printf("%s\n", &buf[i]);
strncat(word,&buf[i], 1);
}
//printf("%s", &buf[i]);
//printf("%d -", i);
i++;
} while(buf[i] != '\0');
printf("%s\n", leaderRec.what);
printf("%s\n", leaderRec.from);
printf("%d\n", leaderRec.value);
return 0;
}
输出:
leadervalue
host__vdso_get
9569
有更好的方法吗?
如何避免示例输出中第二次打印时的垃圾输出
但也存在一些问题。真讨厌。2在中打印结构时
我在字符串后接收垃圾输出的结尾
正如注释中所指出的那样,“垃圾”打印是因为您在终止字符串时没有正确地为null。例如,在调用strncat
之前,您没有将word
初始化为空字符串(单个空字符)
有更好的方法吗
当然可以。我建议使用strtok
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct received {
char what[50];
char from[50];
int value;
};
int main(void)
{
char buf[] = "leadervalue host 9569";
struct received leaderRec;
/* Initialize leaderRec here */
memset(&leaderRec, 0, sizeof(leaderRec));
char *s = strdup(buf);
char *p = strtok(s, " ");
if (p != NULL) {
strncpy(leaderRec.what, p, sizeof(leaderRec.what));
p = strtok(NULL, " ");
}
if (p != NULL) {
strncpy(leaderRec.from, p, sizeof(leaderRec.from));
p = strtok(NULL, " ");
}
if (p != NULL) {
leaderRec.value = atoi(p);
}
free(s);
printf("%s %s %d\n", leaderRec.what, leaderRec.from, leaderRec.value);
return 0;
}
#包括
#包括
#包括
收到结构{
查什么[50];
来自[50]的字符;
int值;
};
内部主(空)
{
char buf[]=“leadervalue主机9569”;
结构接收leaderRec;
/*在此处初始化leaderRec*/
memset(&leaderRec,0,sizeof(leaderRec));
char*s=strdup(buf);
char*p=strtok(s,“”);
如果(p!=NULL){
strncpy(leaderRec.what,p,sizeof(leaderRec.what));
p=strtok(空,“”);
}
如果(p!=NULL){
strncpy(leaderRec.from,p,sizeof(leaderRec.from));
p=strtok(空,“”);
}
如果(p!=NULL){
leaderRec.value=atoi(p);
}
免费的;
printf(“%s%s%d\n”,leaderRec.what,leaderRec.from,leaderRec.value);
返回0;
}
但也存在一些问题。真讨厌。2在中打印结构时
我在字符串后接收垃圾输出的结尾
正如注释中所指出的那样,“垃圾”打印是因为您在终止字符串时没有正确地为null。例如,在调用strncat
之前,您没有将word
初始化为空字符串(单个空字符)
有更好的方法吗
当然可以。我建议使用strtok
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct received {
char what[50];
char from[50];
int value;
};
int main(void)
{
char buf[] = "leadervalue host 9569";
struct received leaderRec;
/* Initialize leaderRec here */
memset(&leaderRec, 0, sizeof(leaderRec));
char *s = strdup(buf);
char *p = strtok(s, " ");
if (p != NULL) {
strncpy(leaderRec.what, p, sizeof(leaderRec.what));
p = strtok(NULL, " ");
}
if (p != NULL) {
strncpy(leaderRec.from, p, sizeof(leaderRec.from));
p = strtok(NULL, " ");
}
if (p != NULL) {
leaderRec.value = atoi(p);
}
free(s);
printf("%s %s %d\n", leaderRec.what, leaderRec.from, leaderRec.value);
return 0;
}
#包括
#包括
#包括
收到结构{
查什么[50];
来自[50]的字符;
int值;
};
内部主(空)
{
char buf[]=“leadervalue主机9569”;
结构接收leaderRec;
/*在此处初始化leaderRec*/
memset(&leaderRec,0,sizeof(leaderRec));
char*s=strdup(buf);
char*p=strtok(s,“”);
如果(p!=NULL){
strncpy(leaderRec.what,p,sizeof(leaderRec.what));
p=strtok(空,“”);
}
如果(p!=NULL){
strncpy(leaderRec.from,p,sizeof(leaderRec.from));
p=strtok(空,“”);
}
如果(p!=NULL){
leaderRec.value=atoi(p);
}
免费的;
printf(“%s%s%d\n”,leaderRec.what,leaderRec.from,leaderRec.value);
返回0;
}
请像人类阅读一样编写代码。请注意,您使用malloc()
的方式使您看起来不明白malloc()
的用途。您的strncpy()
可能有点错误。我从来没有用过它,因为我觉得最好用memcpy()
手工编写'\0'
。这对我来说是个谜-word=(char*)malloc(sizeof(char)*50);/*…*/免费(字);word=(char*)malloc(sizeof(char)*50)代码>使用strtok
。。。。。你可以用word=malloc(50)实现*字='\0'代码>请像人类阅读一样编写代码。请注意,您使用malloc()
的方式使您看起来不明白malloc()
的用途。您的strncpy()
可能有点错误。我从来没有用过它,因为我觉得最好用memcpy()
手工编写'\0'
。这对我来说是个谜-word=(char*)malloc(sizeof(char)*50);/*…*/免费(字);word=(char*)malloc(sizeof(char)*50)代码>使用strtok
。。。。。你可以用word=malloc(50)实现*字='\0'代码>没错,我会删除我的第一条评论:我在评论之前给出了UV。没错,我会删除我的第一条评论:我在评论之前给出了UV。