Solved

Writing struct to the file

Posted on 2000-03-05
2
283 Views
Last Modified: 2013-11-20
I tried to write and read the _RAL_ITEM
struct to file,but when read it,
the _RAL_ITEM struct from file contains
NULL data(such as strings...)
Here is definition:

typedef struct _TIME
{
      int iMin;
      int iHours;
      int iDay;
      int iMonth;
      int iYear;
};
typedef struct _RAL_ITEM
{
      int iIndex;
      UINT nType;
      CString sComment;
      CString sData      ;            
      _TIME time;
      BOOL bStopped;
      BOOL bDone;
};

I tried to write it to file this way:
 f.Write((void*)&item,sizeof(item));
-where f is opened file for writing
 and item is filled struct.

And read it this way:
 _RAL_ITEM item;
 f.Read((void*)&item,sizeof(item));
- where f is opened file for reading.
- after reading item.sData or
  item.sComment are empty(all data from
  struct are empty!!!

How to read it!!!
0
Comment
Question by:radek_s
2 Comments
 
LVL 9

Accepted Solution

by:
ShaunWilde earned 200 total points
ID: 2584830
Your problem is that sComment and sData are CStrings and cannot be used in this way - you must either use archiving (CArchive) or replace sComment and sData
with fixed length char arrays

typedef struct _RAL_ITEM
{
int iIndex;
UINT nType;
char sComment[32]; // you decide the max length required for this field
char sData[128] ; // you decide the max length required here also
_TIME time;
BOOL bStopped;
BOOL bDone;
};

you can then use the methods you have described

0
 

Author Comment

by:radek_s
ID: 2585529
Adjusted points to 200
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now