Char buffer error in a Structure

Discussion in 'C Programming' started by ...vagrahb, Apr 19, 2008.

  1. ...vagrahb

    ...vagrahb Guest

    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.
     
    ...vagrahb, Apr 19, 2008
    #1
    1. Advertising

  2. ...vagrahb

    ...vagrahb Guest

    On Apr 19, 12:53 pm, (Gordon Burditt) wrote:
    > > 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.
     
    ...vagrahb, Apr 19, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    558
  2. wwj
    Replies:
    24
    Views:
    2,522
    Mike Wahler
    Nov 7, 2003
  3. Sathyaish

    char* gets(char* buffer)

    Sathyaish, Oct 5, 2004, in forum: C Programming
    Replies:
    9
    Views:
    431
    CBFalconer
    Oct 5, 2004
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,066
    Ian Collins
    May 9, 2006
  5. VasandGVD
    Replies:
    5
    Views:
    4,655
    CBFalconer
    Nov 28, 2008
Loading...

Share This Page