Solved

Save from and load to Array of Struct

Posted on 2001-08-20
11
392 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

743 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