fwrite() equivalent

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Hi,

how can you write a 'myobject' to a file with one command, as in C :

struct MyStruct
{
char m_name[20];
int m_age;
} myS;
myS.m_age = 20;
strcpy(myS.m_name, "Bob");

FILE* fp = fopen("test.txt", "w");
fwrite(&myS, sizeof(struct MyStruct), 1, fp); --> WRITE IN ONE GO

thnx

Chris
 
fwrite and fread are inherently unsafe operations because garbage values
from the file can be used to possibly initialize memory pointers or overflow
various other data types. You can, however, marshal a byte array onto a
structure and vice versa. The System.Runtime.InteropServices.Marshal class
appears to have everything you would need to operate in an *unsafe* context
if you chose to do this. I used a similar feature when reading/writing ID3 tags
as part of my MP3 library.
 
Back
Top