结构体付给字符串出错

aierlankafei456 2009-01-06 01:40:43

typedef struct SNDSIGUNIT
{
unsigned short Length;
unsigned char Type;
unsigned char LinkNo;
unsigned char PktBuf[1024];
}SNDSIGUNIT;

char sndpack[2048];

memcpy(sndpack, &sndunit, sndunit.Length);

以上是程序片段,前面结构体打印的信息都对,但是这样付给char sndpack[2048]时,结果是一个乱码的字符,而且只收到一个字符。
请问各位大虾为什么?
...全文
96 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
微笑的撒旦 2009-01-07
  • 打赏
  • 举报
回复
没发现错误。
blacktj 2009-01-06
  • 打赏
  • 举报
回复
觉得LZ查看的方式不对, 你说只能看到一个可见字符??
你copy过去的是个结构体,怎么能以字符方式去查看呢?直接查看sndpack内存
你结构的第一个成员是整形, 当然不能以字符串打印出来
yellowhwb 2009-01-06
  • 打赏
  • 举报
回复
“memcpy(sndpack, &sndunit, sndunit.Length);”
你把一个结构体拷贝到一个char sndpack[2048];数组里,是何用意??debug一下看看sndunit.Length的值是否正确!
aierlankafei456 2009-01-06
  • 打赏
  • 举报
回复
请问fibbery,这样子的话写给驱动可以吗,不会因为0影响吧?
aierlankafei456 2009-01-06
  • 打赏
  • 举报
回复
#include "ss7send.h"

struct STRUCTCONF conf[8];
struct SNDSIGUNIT sndunit;

int devInfo(char []);
void sendInfo(char [], int);

/*The function saveStruct send .conf to struct*/
void saveStruct(void)
{
FILE *fpconf;
char line[100];
int flag = 0;
char *token;
char *seps;
int i;

if((fpconf = fopen("ss7send.conf", "r+")) == NULL)
{
printf("The file ss7send.conf open error! \n");
}

seps=":";

while(! feof(fpconf))
{
fgets(line, sizeof(line), fpconf);
// printf("the line is %s \n",line);
if(line[0] == '#')
continue;
token = strtok(line, seps);
i = atoi(token);
printf("the id is %d\n", i);
conf[flag].id = i;

token = strtok(NULL, seps);
strcpy(conf[flag].FISU, token);
printf("the FISU is %s\n", conf[flag].FISU);

token = strtok(NULL,seps);
strcpy(conf[flag].type, token);
printf("the type is %s\n", conf[flag].type);

token = strtok(NULL, seps);
i = atoi(token);
conf[flag].timeout = i;
printf("the timeout is %d\n", conf[flag].timeout);

flag++;
if(flag == 8)
break;
}
fclose(fpconf);
}
int main(void)
{
FILE *fpdat;
char s[1024];
int flag = 0;
int sock;

if((fpdat = fopen("Adate040612.dat","r+")) == NULL)
{
printf("Adate040612.dat file open error! \n");
}

saveStruct();
// sleep(2);
while(! feof(fpdat))
{
memset(s, 0, sizeof(s));
fgets(s, sizeof(s), fpdat);
// printf("The line message is %s\n",s);
flag++;
if(flag % 2 == 1)
{
sock = devInfo(s);
// printf("the sock is %d\n",sock);
}
if(flag % 2 == 0)
{
sendInfo(s, sock);
// printf("the line number is %d\n",flag);
}
sleep(1);
}
fclose(fpdat);
}

/*The function devInfo return the sock id depend on send type */
int devInfo(char line[1024])
{
int i;

for(i=0; i<8; i++)
{
if((line[0] == conf[i].type[0]) && (line[1] == conf[i].type[1]))
return(conf[i].id);
if((line[0]== ' ') && (line[1] == '0'))
return(conf[0].id);
}
return(-1);
}
/*The function sendinfo send package to special sock */
void sendInfo(char ss[1024], int sockid)
{
int fd;
ssize_t wrnum;
struct timeval tmset;
int tmout;
char sndpack[2048];

tmset.tv_sec = 0;
//tmset.tv_usec = conf[sockid].timeout *1000;
tmset.tv_usec = conf[sockid].timeout;

memset(&sndunit, 0, sizeof(sndunit));
strcpy(sndunit.PktBuf, ss);
printf("the PktBuf content is %s\n", sndunit.PktBuf);
sndunit.Type = 0;
sndunit.LinkNo = (unsigned char)sockid;
printf("the sockid is %d \n", sndunit.LinkNo);
sndunit.Length = strlen(ss) + 4;
printf("the sndunit Length is %d\n", sndunit.Length);
memcpy(sndpack,&sndunit, sndunit.Length);

if((fd = open("/var/dev/ss7Gw", O_WRONLY)) < 0)
{
perror("Device open error \n");
printf("The file device open error! \n");
}
if((tmout = select(0, NULL, NULL, NULL, &tmset)) == 0)
{
if((wrnum = write(fd, sndpack, sndunit.Length)) < 0)
{
printf("write MSU to sock error!\n");
}

memset(sndpack, 0, sizeof(sndpack));
memset(&sndunit, 0, sizeof(sndunit));

strcpy(sndunit.PktBuf, conf[sockid].FISU);
sndunit.Type = 1;
sndunit.LinkNo = (unsigned char)sockid;
sndunit.Length = sizeof(conf[sockid].FISU)+4;
printf("the sununit length is %d \n", sndunit.Length);
memcpy(sndpack, &sndunit, sndunit.Length);

if((wrnum = write(fd, sndpack, sizeof(sndpack)))< 0)
{
printf("write FISU to sock error!\n");
}

// printf("write to file the message is %s",ss);
printf("===============\n");
}
close(fd);
}
aierlankafei456 2009-01-06
  • 打赏
  • 举报
回复
# ifndef __SS7SEND_H
# define __SS7SEND_H

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <signal.h>
#include <fcntl.h>

typedef struct STRUCTCONF
{
int id;
char FISU[3];
char type[10];
int timeout;
}STRUCTCONF;

#pragma pack(1)

typedef struct SNDSIGUNIT
{
unsigned short Length;
unsigned char Type;
unsigned char LinkNo;
unsigned char PktBuf[1024];
}SNDSIGUNIT;
#pragma pack(4)

# endif /* __SS7SEND_H */
yellowhwb 2009-01-06
  • 打赏
  • 举报
回复
memcpy(sndpack, &sndunit.PktBuf, sndunit.Length);
这样试试
fibbery 2009-01-06
  • 打赏
  • 举报
回复
sndunit变量应该是SNDSIGUNIT类型吧?

内存复制是真个结构体,结构体内的数据,不能保证是可见字符,所以,输出结果不能保证不是乱码。

只显示一个字符,是因为Length字段的值,恰好在第二个字节为0,字符串结束。而第一个字符为结构体数据的长度,你可以看一下,对应ASCII中的哪一个字符。
waizqfor 2009-01-06
  • 打赏
  • 举报
回复
char sndpack[2048];//哥们字符串不能怎么定义啊
char *sndpack[2048]
tangshuiling 2009-01-06
  • 打赏
  • 举报
回复
把代码搞完整一点,至少得说明变量sndunit是什么,是struct SNDSIGUNIT sndunit???
观看&sndunit、sndunit.length的值

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧