Solved

Writing struct to the file

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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

617 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