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. problem with writing files

problem with writing files

Scheduled Pinned Locked Moved C#
helpannouncement
4 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.
  • S Offline
    S Offline
    swatgodjr
    wrote on last edited by
    #1

    i have written a program that keeps track of records in a data file and came across a small problem. writing to files for most part works fine except when i go to update entries, if the new entry is bigger than the old one it starts overwriting data ahead of the entry. if someone could give me soem advice on how i can prevent this i would very much appreciate it.

    D 1 Reply Last reply
    0
    • S swatgodjr

      i have written a program that keeps track of records in a data file and came across a small problem. writing to files for most part works fine except when i go to update entries, if the new entry is bigger than the old one it starts overwriting data ahead of the entry. if someone could give me soem advice on how i can prevent this i would very much appreciate it.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Text file, correct?? The only way do this is to load the entire file into memory (an array of strings works), make the changes to the in-memory copy, then write then entire file back out, overwriting what's on disk. You can even write it back out to a temporary file, then delete the original file, and rename the temp file to the original filename.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      S 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Text file, correct?? The only way do this is to load the entire file into memory (an array of strings works), make the changes to the in-memory copy, then write then entire file back out, overwriting what's on disk. You can even write it back out to a temporary file, then delete the original file, and rename the temp file to the original filename.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        S Offline
        S Offline
        swatgodjr
        wrote on last edited by
        #3

        thats sort of what i was trying to avoid was a lot of extra work, way i once did it in c++ was i capped all my line sizes so i knew the max size i could work with but did not want this limitation in my new c# stuff, i appreciate the response and i have already thought about doing something very much like you mentioned.

        D 1 Reply Last reply
        0
        • S swatgodjr

          thats sort of what i was trying to avoid was a lot of extra work, way i once did it in c++ was i capped all my line sizes so i knew the max size i could work with but did not want this limitation in my new c# stuff, i appreciate the response and i have already thought about doing something very much like you mentioned.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          swatgodjr wrote:

          way i once did it in c++ was i capped all my line sizes so i knew the max size i could work

          This only works if every line is exactly the same length in the file. Then it's easy to calculate the offset and you know how much space you have left. But, if you use the default methods of writing files, then each line is not going to be the same length.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          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