Solved

Save from and load to Array of Struct

Posted on 2001-08-20
11
393 Views
Last Modified: 2013-12-14
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
0
Comment
Question by:prstein
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6407540
If you want to keep these structs containing MFC CStrings, I'm afraid that you'll have to go for the MFC serialization concept...
0
 
LVL 1

Author Comment

by:prstein
ID: 6407625
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
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6407756
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();


0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6407775
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();
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6407960
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);
     };
}
0
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.

 

Expert Comment

by:Samuel082001
ID: 6408252
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.
0
 
LVL 1

Author Comment

by:prstein
ID: 6408443
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
0
 

Expert Comment

by:Samuel082001
ID: 6408599
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.
0
 
LVL 9

Accepted Solution

by:
ShaunWilde earned 300 total points
ID: 6408830
my fault - I forgot the following calls before f.Close()

     ar.Flush();
     ar.Close();

 
0
 
LVL 1

Author Comment

by:prstein
ID: 6409400
ShaunWilde;

Thanks for persisting through this.  Well done!

Regards,

Paul
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6409496
no probs - glad to help
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

Suggested Solutions

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

895 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