?
Solved

Save from and load to Array of Struct

Posted on 2001-08-20
11
Medium Priority
?
405 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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 1200 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  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 …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

777 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