[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Save from and load to Array of Struct

Posted on 2001-08-20
11
Medium Priority
?
416 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

834 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