Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Read & Write to C Flat File in C#

Read & Write to C Flat File in C#

Scheduled Pinned Locked Moved C#
csharpc++
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KeithF
    wrote on last edited by
    #1

    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

    J 1 Reply Last reply
    0
    • K KeithF

      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

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      So lets get this straight - you've found the right way to do it, but you want an easy way?

      K 1 Reply Last reply
      0
      • J J4amieC

        So lets get this straight - you've found the right way to do it, but you want an easy way?

        K Offline
        K Offline
        KeithF
        wrote on last edited by
        #3

        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

        J 1 Reply Last reply
        0
        • K KeithF

          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

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          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[^]

          K 1 Reply Last reply
          0
          • J J4amieC

            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[^]

            K Offline
            K Offline
            KeithF
            wrote on last edited by
            #5

            Thanks J4amieC, that looks to be what i'm looking for.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups