I have written a simple small program to read a file with following format using snprintf,
skip first 15 chars , next 9 chars are sequence number, next 2 char is message and so on.
I am interested in sequence number and message i.e. from char no 16 to 26;
below is the program. It does not reads last char for each field. It reads 8 chars instead of 9 for sequence number and and 1 byte instead of 2 for message.
#include<stdio.h>
typedef struct
{
char seqno[9];
char msg[2];
}Header_T;
int main()
{
char buf[64]={'\0'};
FILE *fp;
int i = 0;
Header_T hdr1;
int skipbytes = 15;
fp=fopen("asdf", "r");
if (fp == NULL)
{
printf("FILE OPEN ERROR\n");
}
printf("--sequence--msg--\n");
while( fgets( buf, sizeof(buf), fp ) != NULL )
{
i=skipbytes;
snprintf(hdr1.seqno, 9, "%s", (buf+i));
i+=sizeof(hdr1.seqno);
snprintf(hdr1.msg, 2, "%s", (buf+i));
i=0;
printf("--%s--%s--\n", hdr1.seqno, hdr1.msg);
memset(buf, '\0', 64 );
}
fclose(fp);
return 0 ;
}
part of file contents is as follows
201301082323458000000001H QB234
201301082323558000000002J QB234
201301082323658000000003N QB234
201301082323758000000004JRQB234
201301082333458000000010JSQB234
so expected output is
--sequence--msg--
--000000001--H --
--000000002--J --
--000000003--N --
--000000004--JR--
--000000010--JS--
But Instead I am getting output as
--seqno--msgtype--
--00000000--H--
--00000000--J--
--00000000--N--
--00000000--J--
--00000001--J--
Can anyone explain this behavior and how to fix it?
Instead of snprintf I tried the same program using for loop character-by-character assignment and program works fine; but for that I need to add some fillers for byte alignment in structure.
I also tried using pragma pack() but it also makes no difference.
I am using gcc 4.4.3 on ubuntu 64 bit machine