Solved

Writing struct to the file

Posted on 2000-03-05
2
282 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 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

12 Experts available now in Live!

Get 1:1 Help Now