gtsoulou
asked on
Insert data in a file
Hi. I want to insert some data in a file. The file allready exists and has data. So I want to write in this file but not at the end of file, whithout overwritting the existing data in this possition and without using a temporary file. I tried this code but doesn't works because the new data overwrites the old:
Dim fs As FileStream = New FileStream("data.bin", FileMode.OpenOrCreate, FileAccess.Write)
Dim w As BinaryWriter = New BinaryWriter(fs) ' create a BinaryWriter
w.BaseStream.Seek(4, SeekOrigin.Begin) ' seek the beginning of the file after 4bytes
w.Write(CType(CInt(TextBox 1.Text), Int32))
fs.Flush()
fs.Close()
The code above overwrites the data from 4th byte to 8th byte (because Int32 is 4 bytes) and writes there the new data.
Dim fs As FileStream = New FileStream("data.bin", FileMode.OpenOrCreate, FileAccess.Write)
Dim w As BinaryWriter = New BinaryWriter(fs) ' create a BinaryWriter
w.BaseStream.Seek(4, SeekOrigin.Begin) ' seek the beginning of the file after 4bytes
w.Write(CType(CInt(TextBox
fs.Flush()
fs.Close()
The code above overwrites the data from 4th byte to 8th byte (because Int32 is 4 bytes) and writes there the new data.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by holli
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
TheLearnedOne
EE Cleanup Volunteer