Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Writing struct to the file

Posted on 2000-03-05
2
Medium Priority
?
290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Accepted Solution

by:
ShaunWilde earned 800 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

688 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