#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Brazil
    Posts
    15
    Rep Power
    0

    fgets / strncpy - newbie question


    Im trying to make an application (C++ Builder) to register some data from several clients, using one file, kind of database. I got some doubts about the number of bytes I should use on each part like you can see below:

    struct DadosCadastro
    {
    char chrData[9];
    char chrProntuario[9]; // In the form, each field has 8, 8 and
    char chrNome[51]; // 50 as MaxLength (Why +1 for each ?)
    };

    struct DadosCadastro Cadastro;
    int intRegistro,intPosicao,intTotalRegistros;
    char chrDados[69],chrInclusao; // Should this number be 69
    FILE *fpCadastro; // (9+9+51) or 66 (8+8+50)

    void LeRegistro()
    {
    fgets(chrDados,69,fpCadastro); // 69 or 66 ???
    strcpy(Cadastro.chrData,chrDados,8); // 8 or 9 ??
    Cadastro.chrData[8] = '\0'; // 8 or 9 ??
    strcpy(Cadastro.chrProntuario,&chrDados[8],8);
    Cadastro.chrProntuario[8] = '\0';
    strcpy(Cadastro.chrNome,&chrDados[16],50);
    Cadastro.chrNome[50] = '\0';

    This is just a piece of the whole code but my doubt is located on these points. Why one byte more if Im not using this extra byte after ?? All this because of an example I got from a book...

    I hope you got the idea... Thanks for helping me !!!! Im just a begginer...



    // In the form each field has 8, 8 and // 50 as MaxLength (Why +1 for each ?) // Should this number be 69 // (9+9+51) or 66 (8+8+50)
  2. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    the extra byte is for the NULL terminator. C strings need to be terminated by a '\0', this lets the compiler know where the string ends. even though '\0' looks like 2 characters, it is actually a special value and it is referred to as NULL. hope that helps
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Brazil
    Posts
    15
    Rep Power
    0
    I got it, but there is more like


    //** This part is alright ***//

    struct DadosCadastro
    {
    char chrData[9];
    char chrProntuario[9];
    char chrNome[51];
    };


    //*** So, in this part, where you see the number 69 should I consider this extra byte for every variable being the total 69 as you see ?? ***///

    struct DadosCadastro Cadastro;
    int intRegistro,intPosicao,intTotalRegistros;
    char chrDados[69],chrInclusao;
    FILE *fpCadastro;



    void LeRegistro()
    {
    fgets(chrDados,69,fpCadastro);
    strcpy(Cadastro.chrData,chrDados,8);

    //*** So, here the last byte should the 9th not the 8th as you see below ?? Am I right *** //

    Cadastro.chrData[8] = '\0';

    strcpy(Cadastro.chrProntuario,&chrDados[8],8);
    Cadastro.chrProntuario[8] = '\0';
    strcpy(Cadastro.chrNome,&chrDados[16],50);
    Cadastro.chrNome[50] = '\0';

    Sorry about these questions because I got this from one book I was using to study, but it has been frustating due to these reasons... Thank you

IMN logo majestic logo threadwatch logo seochat tools logo