Click here to Skip to main content
15,901,373 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: error PRJ0019: A tool returned an error code from "Registering ActiveX Control..." Issue Pin
KarstenK16-Dec-09 22:38
mveKarstenK16-Dec-09 22:38 
Questionreading writing to a file system Pin
hrishiS16-Dec-09 17:44
hrishiS16-Dec-09 17:44 
AnswerRe: reading writing to a file system Pin
Adam Roderick J16-Dec-09 17:56
Adam Roderick J16-Dec-09 17:56 
GeneralRe: reading writing to a file system [modified] Pin
hrishiS16-Dec-09 18:23
hrishiS16-Dec-09 18:23 
GeneralRe: reading writing to a file system Pin
Adam Roderick J16-Dec-09 21:51
Adam Roderick J16-Dec-09 21:51 
AnswerRe: reading writing to a file system Pin
Rajesh R Subramanian16-Dec-09 17:59
professionalRajesh R Subramanian16-Dec-09 17:59 
GeneralRe: reading writing to a file system Pin
hrishiS16-Dec-09 18:18
hrishiS16-Dec-09 18:18 
AnswerRe: reading writing to a file system Pin
CPallini16-Dec-09 22:03
mveCPallini16-Dec-09 22:03 
I already gave you some code, but OK, it was incomplete.
Let's try a plain, not object oriented, C-Like approach:
#include <stdio.h>


enum SerialType
{
  eTYPE_UNRECOGNIZED,
  eTYPE_A,
  eTYPE_B,
  eTYPE_C,
  eTYPES
};

struct A
{
  int id;
  int data;
};

struct B
{
  int id;
  float data;
};

struct C
{
  int id;
  double data;
  int data2;
}; 


size_t getSize(SerialType st)
{
  switch( st)
  {
  case eTYPE_A: 
    return sizeof(A);
  case eTYPE_B: 
    return sizeof(B);
  case eTYPE_C: 
    return sizeof(C);
  default:
    return 0;
  }
}

bool store( SerialType st, void * pData, FILE * fp)
{
  int type = st;
  size_t size = getSize(st);

  if (!fp || !pData || !size) return false;
  fwrite(&type, sizeof(type), 1, fp);
  fwrite(pData, size,1,fp);
  return true;
}

SerialType readType( FILE * fp)
{
  SerialType type;
  if (!fp ) return eTYPE_UNRECOGNIZED;

  if ( fread(&type, sizeof(type), 1, fp) == 0 ) return eTYPE_UNRECOGNIZED;
  return type;
}

bool readData( SerialType st, void * pData, FILE * fp)
{
  size_t size = getSize(st);
  if ( !fp || !pData || !size ) return false;
  fread(pData, size, 1, fp);
  return true;
}

void main()
{
  SerialType type;

  A a[2];
  B b;
  C c1,c2;

  A * pa=NULL;
  B * pb=NULL;
  C * pc=NULL;

  // define some variables
  a[0].data = 5;
  a[0].id = 12;

  a[1].data = 6;
  a[1].id = 13;
  
  b.data=25.75f;
  b.id=100;


  c1.data = 27;
  c1.data2=35;
  c1.id = 7;

  c2.data=1000.372;
  c2.data2=1000;
  c2.id=1;

 
  FILE * fp = fopen("data.raw", "wb");

  // store some of the defined variables
  store(eTYPE_C, &c2, fp);
  store(eTYPE_A, &a[1], fp);
  store(eTYPE_B, &b, fp);
  store(eTYPE_A, &a[0], fp);

  fclose(fp);



  // read the variables from the storage, printing their values.
  fp = fopen("data.raw", "rb");

  while ((type=readType(fp)) != eTYPE_UNRECOGNIZED)
  {
    switch (type)
    {
    case eTYPE_A:
      pa = new A();
      readData(type, pa, fp);
      printf("type A,  id=%d, data=%d.\n", pa->id, pa->data);
      delete pa;
      break;
    case eTYPE_B:
      pb = new B();
      readData(type, pb, fp);
      printf("type B,  id=%d, data=%g.\n", pb->id, pb->data);
      delete pb;
    break;
    case eTYPE_C:
      pc = new C();
      readData(type, pc, fp);
      printf("type C,  id=%d, data=%g, data2=%d.\n", pc->id, pc->data, pc->data2);
      delete pb;
    break;
    }
  }
  fclose(fp);
}


Please note, an object oriented approach would be better: serialization fits well into OOP paradigm (however, any attempt about left to the reader...).

Smile | :)

If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.

This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke

[My articles]

QuestionUse theme elements with alpha Pin
Ivo Beltchev16-Dec-09 13:39
Ivo Beltchev16-Dec-09 13:39 
AnswerRe: Use theme elements with alpha Pin
Adam Roderick J16-Dec-09 17:05
Adam Roderick J16-Dec-09 17:05 
GeneralRe: Use theme elements with alpha Pin
Ivo Beltchev16-Dec-09 17:31
Ivo Beltchev16-Dec-09 17:31 
GeneralRe: Use theme elements with alpha Pin
Rozis17-Dec-09 9:56
Rozis17-Dec-09 9:56 
AnswerRe: Use theme elements with alpha Pin
Code-o-mat16-Dec-09 20:25
Code-o-mat16-Dec-09 20:25 
GeneralRe: Use theme elements with alpha Pin
Ivo Beltchev16-Dec-09 20:42
Ivo Beltchev16-Dec-09 20:42 
GeneralRe: Use theme elements with alpha Pin
Code-o-mat16-Dec-09 20:59
Code-o-mat16-Dec-09 20:59 
Questioncan I run notepad.exe inside View window? Pin
rambojanggoon16-Dec-09 9:20
rambojanggoon16-Dec-09 9:20 
AnswerRe: can I run notepad.exe inside View window? Pin
Nemanja Trifunovic16-Dec-09 9:40
Nemanja Trifunovic16-Dec-09 9:40 
Questionminimize notification of MFC application Pin
kasi1416-Dec-09 7:45
kasi1416-Dec-09 7:45 
AnswerRe: minimize notification of MFC application Pin
Abhi Lahare16-Dec-09 9:52
Abhi Lahare16-Dec-09 9:52 
QuestionHow to get a thread's exit code Pin
masnu16-Dec-09 5:24
masnu16-Dec-09 5:24 
AnswerRe: How to get a thread's exit code Pin
«_Superman_»16-Dec-09 5:38
professional«_Superman_»16-Dec-09 5:38 
AnswerRe: How to get a thread's exit code Pin
Rajesh R Subramanian16-Dec-09 7:56
professionalRajesh R Subramanian16-Dec-09 7:56 
GeneralRe: How to get a thread's exit code Pin
masnu16-Dec-09 8:07
masnu16-Dec-09 8:07 
AnswerRe: How to get a thread's exit code Pin
Rajesh R Subramanian16-Dec-09 8:25
professionalRajesh R Subramanian16-Dec-09 8:25 
AnswerRe: How to get a thread's exit code Pin
Rajesh R Subramanian16-Dec-09 8:30
professionalRajesh R Subramanian16-Dec-09 8:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.