Read & Write to C Flat File in C#
-
Hi Guys, I have a structure in C / C++ as follows: typedef struct MyDataFields{ char Id[12]; char Name[32]; char Address[4][32]; char DateLastSale[12]; long TransCount; int PointsToDate; char Spare[20]; }MyData; This structure is written to a flat file in one fwrite call and is read in using fread. However this is legacy stuff now and we are about to undertake a re-write but we need to be able to read these files in C# .Net 2.0. I have searched the web for days but have got nowhere. I have saw 1 or 2 examples where it has been said to create a class to represent the struct, there must be however a simpler way of doing this. Anyone done this before that can point me in the right direction or some sample code based on the struct above. Thanks in Advance Keith
-
Hi Guys, I have a structure in C / C++ as follows: typedef struct MyDataFields{ char Id[12]; char Name[32]; char Address[4][32]; char DateLastSale[12]; long TransCount; int PointsToDate; char Spare[20]; }MyData; This structure is written to a flat file in one fwrite call and is read in using fread. However this is legacy stuff now and we are about to undertake a re-write but we need to be able to read these files in C# .Net 2.0. I have searched the web for days but have got nowhere. I have saw 1 or 2 examples where it has been said to create a class to represent the struct, there must be however a simpler way of doing this. Anyone done this before that can point me in the right direction or some sample code based on the struct above. Thanks in Advance Keith
-
So lets get this straight - you've found the right way to do it, but you want an easy way?
Hi J4amieC <blockquote class="FQ"><div class="FQA">J4amieC wrote:</div>So lets get this straight - you've found the right way to do it, but you want an easy way? </blockquote> No i'm not saying that, i'm simply saying that its a lot easier in C where you read the struct into memory modify it and write it back out again. I was thinking that it should be as straight foward in C#. Regards Keith
-
Hi J4amieC <blockquote class="FQ"><div class="FQA">J4amieC wrote:</div>So lets get this straight - you've found the right way to do it, but you want an easy way? </blockquote> No i'm not saying that, i'm simply saying that its a lot easier in C where you read the struct into memory modify it and write it back out again. I was thinking that it should be as straight foward in C#. Regards Keith
Yes its just as easy in C#, we write a struct (which must be serializable) and we serialize it to a file and deserialize it back to an instance in memory. Granted its not as easy as 1 line call to whatever your C++ functions were - but in essence its the same. Check out a Binary Serialization[^]
-
Yes its just as easy in C#, we write a struct (which must be serializable) and we serialize it to a file and deserialize it back to an instance in memory. Granted its not as easy as 1 line call to whatever your C++ functions were - but in essence its the same. Check out a Binary Serialization[^]