[2005] Reading parts of a file in binary and overwriting only parts
Ive been using FileStream,BinaryReader, and BinaryWriter for my latest project. All was working great, I was reading RinaryReader.ReadChars() and writing using BinaryWriter.Write(). However my problem arises because I thought .write would overwrite, instead it just inserts/appends. Is there something I could use to overwrite instead of append? Thanks
Re: [2005] Reading parts of a file in binary and overwriting only parts
I would assume you would just have to keep track of where you wish to start writing the new info, and read the data up until then, write the data out, write the new data, and then begin reading again at the last stop location + the number of new bytes you wrote. Of course I haven't done this much at all, so thats not to say there isn't a method or a way to do what you are wanting...
You could also just read all the data into a buffer, and then just modify the buffer at the locations you wish, then just write the buffer out...
Re: [2005] Reading parts of a file in binary and overwriting only parts
They are mp3 files, I want to edit the tags at the beginning of a file. Its about 500 characters in the last piece of info I might need, so thats all I would really need to read or write.
edit*
mp3 files are generally around 3 mb--you probably know that lol
Re: [2005] Reading parts of a file in binary and overwriting only parts
Try using a FileStream, and then try the FileStream.Write method to write the bytes at the offset location you want. I tested writing a few bytes to a small file and the file length did not change at all...
Re: [2005] Reading parts of a file in binary and overwriting only parts
An example of writing data using FileStream and its write method is below. Just to test, it writes "TestData" at the beginning bytes of the file. This is not what you want to do, of course, but I didn't want to delve into the whole ID3 format specs and try to do it per the filespec, so its just to show how it works.
VB Code:
Dim fs As New System.IO.FileStream("c:\test.mp3", IO.FileMode.OpenOrCreate)
Dim TestData() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes("TestData".ToCharArray)
fs.Seek(0, IO.SeekOrigin.Begin)
fs.Write(TestData, 0, TestData.Length)
fs.Close()
I believe it overwrites whatever was there with whatever you write to it, with the FileStream.Write method...