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