Link to home
Start Free TrialLog in
Avatar of prstein
prstein

asked on

Save from and load to Array of Struct

Hello All;

I am relatively new to C++ programming and having a little difficulty...

I have an Array of Struct, consisting of about 15 CStrings and 10 booleans.  The array as 20 elements.

I am trying to figure out the simplest way to save the data in this array to a file, then later reload the array with the data from the file.

In case it's relevant, I'm using Microsoft eVC++ for the Pocket PC.

Thanks,

Paul
Avatar of jkr
jkr
Flag of Germany image

If you want to keep these structs containing MFC CStrings, I'm afraid that you'll have to go for the MFC serialization concept...
Avatar of prstein
prstein

ASKER

Hello jkr;

Thanks for that hint.  I just took a quick look at serialization in the help file, and it *couldn't* be more tedious than what I'm currently doing (which is converting to and from a csv format).

If you (or anyone) can provide a sample, the points are yours.

Regards,

Paul
for saving

struct
{
  BOOL b;
  CString s;
} xstr;


xstr x[20];


CFile f;
if( !f.Open( pFileName, CFile::modeCreate | CFile::modeWrite ) ) {
   #ifdef _DEBUG
      afxDump << "Unable to open file" << "\n";
      exit( 1 );
   #endif
}
CArchive ar( &f, CArchive::store);

for (int i=0;i<20;i++)
{
  ar << x[i].b;
  ar << x[i].s
}

f.Close();


for loading

CFile f;
if( !f.Open( pFileName, CFile::modeRead ) ) {
  #ifdef _DEBUG
     afxDump << "Unable to open file" << "\n";
     exit( 1 );
  #endif
}
CArchive ar( &f, CArchive::load);

for (int i=0;i<20;i++)
{
 ar >> x[i].b;
 ar >> x[i].s
}

f.Close();
Or, using std C++ output/input operators:

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;


struct c
{
     string a, b;
     bool e, f, g;
};

ostream& operator<< (ostream& o, const c& c1)
{
     o << c1.e << " " << c1.f << " " << c1.g << " ";
     o << c1.a.length() << " " << c1.a;
     return o << c1.b.length() << " " << c1.b;
};

istream& operator>> (istream& in, c& c1)
{
     in >> c1.e >> c1.f >> c1.g;

     size_t size;
     char szBuff[100];

     in >> size;
     in.read(szBuff, size);
     c1.a.assign(szBuff, size);

     in >> size;
     in.read(szBuff, size);
     c1.b.assign(szBuff, size);
     // etc
     return in;
};
void main(short ac,char **av)
{
     vector<c> vc;
     fstream f("target.file");
     copy(vc.begin(), vc.end(), ostream_iterator<c>(f, " "));
     vector<c> target;
     while (!f.eof())
     {
          c c1;
          f >> c1;
          target.push_back(c1);
     };
}
I believe for this question, you will have to use the <fstream.h> header file, where you open a file for writing and write the data in the array of structure in the file.
ofstream fout(filename);
fout<<data;
.........
fout.close();
when you want to read from the file,
ifstream fin(filename);
fin>>data;
fin.get(ch);
or
fin.getline(ch, 50, '\n');
anything else, just contact me.
Avatar of prstein

ASKER

Hello ShaunWilde (and everyone);

I tried your suggestion, modified as follows:

void CLibraryEditView::OnButtonSaveLibrary()
{
     CFile f;

     TCHAR* pFileName = _T("\\My Documents\\Library\\TempLib1.lib");

     if( !f.Open( pFileName, CFile::modeCreate | CFile::modeWrite ) )
     {
          #ifdef _DEBUG
               afxDump << "Unable to open file" << "\n";
               exit( 1 );
          #endif
     }

     CArchive ar ( &f, CArchive::store);
     for (int i=0;i<2;i++)
     {
          ar << m_pPV->m_pLib[i].LibIsotopeName;
          ar << m_pPV->m_pLib[i].LibkeV1;
          ar << m_pPV->m_pLib[i].LibYield1;
          ar << m_pPV->m_pLib[i].LibComment1;
          ar << m_pPV->m_pLib[i].LibPri1;
          ar << m_pPV->m_pLib[i].LibSec1;

          ar << m_pPV->m_pLib[i].LibkeV2;
          ar << m_pPV->m_pLib[i].LibYield2;
          ar << m_pPV->m_pLib[i].LibComment2;
          ar << m_pPV->m_pLib[i].LibPri2;
          ar << m_pPV->m_pLib[i].LibSec2;

          ar << m_pPV->m_pLib[i].LibkeV3;
          ar << m_pPV->m_pLib[i].LibYield3;
          ar << m_pPV->m_pLib[i].LibComment3;
          ar << m_pPV->m_pLib[i].LibPri3;
          ar << m_pPV->m_pLib[i].LibSec3;

          ar << m_pPV->m_pLib[i].LibkeV4;
          ar << m_pPV->m_pLib[i].LibYield4;
          ar << m_pPV->m_pLib[i].LibComment4;
          ar << m_pPV->m_pLib[i].LibPri4;
          ar << m_pPV->m_pLib[i].LibSec4;

          ar << m_pPV->m_pLib[i].LibkeV5;
          ar << m_pPV->m_pLib[i].LibYield5;
          ar << m_pPV->m_pLib[i].LibComment5;
          ar << m_pPV->m_pLib[i].LibPri5;
          ar << m_pPV->m_pLib[i].LibSec5;
     }

     f.Close();
}

It compiles and runs fine, but gives the message "an unnamed file was not found".  A file with the appropriate name is created that contains 0 bytes (but does not overwrite or update if the file already exists).

So close, and yet so far...

Paul
I don't think your program code above wrote anything to the file. Maybe that's why the file you created contains 0 bytes. or, there could be a possibility that the directory of the file is written wrongly. This could also cause such an error. Check your code again.
ASKER CERTIFIED SOLUTION
Avatar of ShaunWilde
ShaunWilde

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of prstein

ASKER

ShaunWilde;

Thanks for persisting through this.  Well done!

Regards,

Paul
no probs - glad to help