C++ File system
-
Hi to All, I have 4 rows of data has to be saved in a binary file system . I haven't use it before, Can anyone please help me with this EG: struct A { int a1; int a2; } struct B { int b1; int b2; } struct C { int c1; int c2; } How do I write and read this values from the file system??
----------------------------- I am a beginner
-
Hi to All, I have 4 rows of data has to be saved in a binary file system . I haven't use it before, Can anyone please help me with this EG: struct A { int a1; int a2; } struct B { int b1; int b2; } struct C { int c1; int c2; } How do I write and read this values from the file system??
----------------------------- I am a beginner
You may simply store the
struct
s as row, binary data. For instance (error checking left to the reader):struct A
{
int a1;
int a2;
};A a;
FILE * fp = fopen("data.raw", "wb");
fwrite(&a, sizeof(a), 1, fp);
//..
fclose(fp);
//..For an object oriented approach see Object Serialization [^](for instance
MFC
framework supports it). :)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] -
You may simply store the
struct
s as row, binary data. For instance (error checking left to the reader):struct A
{
int a1;
int a2;
};A a;
FILE * fp = fopen("data.raw", "wb");
fwrite(&a, sizeof(a), 1, fp);
//..
fclose(fp);
//..For an object oriented approach see Object Serialization [^](for instance
MFC
framework supports it). :)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] -
thanks for your reply. But my problem is I have more then 2 structure and I need to search or write at runtime.
----------------------------- I am a beginner
You may do a step toward serialization, storing type info just before actual data, for instance:
enum SER_TYPES
{
eTypeI,
eTypeD,
//..
};struct I
{
int i1, i2;
};
struct D
{
double d1, d2, d3;
};
// ..void store(FILE *fp, I * pi)
{
int t = eTypeI;
fwrite( &t, sizeof(int), 1, fp); // first write type
fwrite( pi, sizeof(*pi), fp); // then write data
}
void store(FILE *fp, D * pd)
{
int t = eTypeD;
fwrite( &t, sizeof(int), 1, fp); // first write type
fwrite( pd, sizeof(*pd), fp); // then write data
}int main()
{
FILE * fp = fopen("data.raw", "wb");
I i,j;
D d,f;
//..
store(fp,i);
store(fp,d);
store(fp,j);
//..
}On reading, you've to first read the type of the stored data and then, according to the type, read the actual data. :)
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] -
You may do a step toward serialization, storing type info just before actual data, for instance:
enum SER_TYPES
{
eTypeI,
eTypeD,
//..
};struct I
{
int i1, i2;
};
struct D
{
double d1, d2, d3;
};
// ..void store(FILE *fp, I * pi)
{
int t = eTypeI;
fwrite( &t, sizeof(int), 1, fp); // first write type
fwrite( pi, sizeof(*pi), fp); // then write data
}
void store(FILE *fp, D * pd)
{
int t = eTypeD;
fwrite( &t, sizeof(int), 1, fp); // first write type
fwrite( pd, sizeof(*pd), fp); // then write data
}int main()
{
FILE * fp = fopen("data.raw", "wb");
I i,j;
D d,f;
//..
store(fp,i);
store(fp,d);
store(fp,j);
//..
}On reading, you've to first read the type of the stored data and then, according to the type, read the actual data. :)
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]thanks , that's the exact one i was looking for...In fact I dont have knowledge on serialization, But i have also reached t that point..Now I am having problem on reading... just for ur information.....my incomplete code is given below I have used class instead of struct class tableSructureUnknown { }; class tableSructure1: public tableSructureUnknown { public: int x; int y; }; class tableSructure2: public tableSructureUnknown { public: int x2; int y2; }; class listTable { public: int id; int size; char tName[20]; }; listTable ArrTableInfo[]= { {TFX_TABLE1,sizeof(tableSructure1), "first"}, {TFX_TABLE2,sizeof(tableSructure2),"Second"}, {TFX_UNKNOWN,0,"Unknown"} }; FILE *ptr_myfile; int writeToFileTable(int tableId,tableSructureUnknown *lpbaseTableClass) { ptr_myfile=fopen("test.bin","w"); if (!ptr_myfile) { printf("Unable to open file!"); return 1; } for(int i = 0; (ArrTableInfo[i].id != TFX_UNKNOWN) || (ArrTableInfo[i].id != tableId) ;i++) { fwrite(&ArrTableInfo[i], sizeof(listTable), 1, ptr_myfile); ptr_myfile=fopen("test.bin","w"); if (!ptr_myfile) { printf("Unable to open file!"); return 0; } fwrite(&lpbaseTableClass, ArrTableInfo[i].size , 1, ptr_myfile);