CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 1999
    Posts
    2

    Write/Read a struct into a file...;-)

    Hi,
    I would like to be able to write and "later!!" read a struct in a file.
    This is my structure :
    struct {
    CString Rep;
    int Size;
    int NbFile;
    }
    I have tried many solutions like with CFile but all the times unsuccessfully :-(

    Thanks i.a. for your help.

    [email protected]


  2. #2
    Join Date
    May 1999
    Location
    WA
    Posts
    236

    Re: Write/Read a struct into a file...;-)

    Your structure needs a "tag" or name then you can write it to a file using basic, low level I/O.


  3. #3
    Join Date
    Oct 1999
    Posts
    2

    Re: Write/Read a struct into a file...;-)

    Thanks,
    But I already know that, I just want by writing this structure explain that I use a CString inside.
    The real source is this one :
    struct Record {
    CString R_rep;
    int R_size;
    int R_nbfile;
    };
    char* pFileName = "test3.dat";
    Record Enregistrement;
    f.Open( pFileName, CFile::modeCreate | CFile::modeWrite, &e ) ;
    Enregistrement.R_rep = Direct;
    Enregistrement.R_size = size;
    Enregistrement.R_nbfile = nbFile;
    // Ecriture dans le fichier
    f.Write((struct Record *) &Enregistrement, sizeof(Enregistrement));
    f.Close();
    // For reading :
    Record Lecture;
    f.Read((struct Record *) &Lecture, sizeof(struct Record));
    f.Close();
    That's all...
    Thanks i.a for any help...
    Ludo



  4. #4
    Join Date
    May 1999
    Location
    CA, USA
    Posts
    586

    Re: Write/Read a struct into a file...;-)

    When you use a CString in a structure, you can't use sizeof to find out how many bytes you want to save or read - I would recommend that instead of using a CString, you use a char[MAX_LENGTH] -- that way your structure will have a constant size and you can use sizeof. An alternative, is to have the first member of the structure be an integer which holds the structure's size:

    struct Record
    {
    int R_iSize;
    CString R_rep;
    int R_size;
    int R_nbfile;
    };

    Then set R_iSize equal to the size of the structure:

    R_iSize = R_rep.GetLength() + sizeof(R_size) + sizeof(R_nbfile);

    after you set the CString value... then use R_iSize as the length parameter in your CFile::Read() or CFile::Write() calls.

    Generally though I use my first suggestion:

    #define MAX_RECORD_STRING 256

    struct Record
    {
    char R_rep[MAX_RECORD_STRING];
    int R_size;
    int R_nbfile;
    };



    Rail

    ------------
    Recording Engineer/Software Developer
    Rail Jon Rogut Software
    http://home.earthlink.net/~railro/
    [email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured