Char buffer error in a Structure

V

...vagrahb

Hi,
I have the following structure

struct Format
{
char x[12];
unsigned char a;
unsigned char b;
unsigned char c;
char y[30];
} __attribute__((__aligned__));

I have a csv file which I am reading the data from which has fields
which match the structure and I tokenize the data read from the file
using strtok. the problem is when I do a strcpy to the the string x
and print the value from the structure for x,
I get the whole string and an extra char at the end of the string. it
is a special char and I am not able to get rid of that special char.
It only happens when I have a token that is exactly 12 chars wide. if
the token being copied into x is smaller than 12 it is fine. But if I
try to copy a token which is 12 bytes long I get an extra char at the
end in x.


I tried all sorts of stuff even memcpy etc and still I end up with
that extra char in x. If I just print the token there is no extra char
but when I copy it into that member of structure and print it I get a
extra token.

sample out put :
Original token
Awsche-tonol
1
9
47
Fiery Legion

Error output from displaying members of the Struct:
Awsche-tonol,1,9,47,Fiery Legion


Any help regarding this please.

Regards.
 
V

...vagrahb

I have the following structure
struct Format
{
char x[12];
unsigned char a;
unsigned char b;
unsigned char c;
char y[30];
} __attribute__((__aligned__));
I have a csv file which I am reading the data from which has fields
which match the structure and I tokenize the data read from the file
using strtok. the problem is when I do a strcpy to the the string x
and print the value from the structure for x,

Show me the code. Particularly, show me where you modify format.a
and how that relates to the point where you put something in x.
I get the whole string and an extra char at the end of the string. it
is a special char and I am not able to get rid of that special char.
It only happens when I have a token that is exactly 12 chars wide. if

If you put 12 characters (not including the \0 terminator) into x,
then it is NOT A String and you should not use the %s format to
attempt to print NOT A Strings. If you used strcpy() (as opposed
to, say, strncpy()) to do it, you also committed a buffer overflow
and invoked the wrath of undefined behaviour by doing so.

After you did that copy, did you perhaps assign something to format.a,
clobbering the string terminator character ?
the token being copied into x is smaller than 12 it is fine. But if I
try to copy a token which is 12 bytes long I get an extra char at the
end in x.

the thing is I have to get the structure size to be 45 bytes and so I
cannot fit in \0 into the string coz then I will have to make the size
of the x as 13 as opposed to 12.

Code to tokenize
split = strtok(buffer,",");
no_tok =0;
j=0;
while(split != NULL)
{

strcpy(temp[j],split);
strcat(temp[j],"\0");
//printf("%s",temp[j]);
j++;
split = strtok(NULL,",");
++no_tok;
}

Code to store into the Structure
if(no_tok != 0)
{
rec++;
strcpy(F.x,temp[0]);
F.a = atoi(temp[1]);
F.b = atoi(temp[2]);
F.c = atoi(temp[3]);

if(no_tok == 5)
strcpy(F.y,temp[4]);
else
if(no_tok == 4)
{
strcpy(F.Guild,"\0");
}
}

and another problem is even if I am using #pragma(1) I am not able to
get the size of the struct to 45 and always ends up as 48.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top