Results 1 to 8 of 8

Thread: [2005] Reading parts of a file in binary and overwriting only parts

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    [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

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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...

  3. #3

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: [2005] Reading parts of a file in binary and overwriting only parts

    I was thinking about the buffer idea, but i thought it would just be too slow. If thats the only way then I guess I can do it.

    Just tested it. Its really slow. Ill look for a better solution
    Last edited by |2eM!x; Dec 3rd, 2006 at 09:39 PM.

  4. #4
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [2005] Reading parts of a file in binary and overwriting only parts

    How much data are you reading in? What size of files?

  5. #5

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    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

  6. #6
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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...

  7. #7

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: [2005] Reading parts of a file in binary and overwriting only parts

    Well see I know its adding because the lines are directly under each other ->



    Im capitalizing the song name in the 2nd one, written just below the first unfortunately..

    Heres how im reading/writing

    VB Code:
    1. br.BaseStream.Seek(0, IO.SeekOrigin.Begin)
    2.                 .ARTIST = IO.Path.GetFileName(IO.Path.GetDirectoryName(fileName))
    3.                 .SONGNAME = IO.Path.GetFileNameWithoutExtension(fileName)
    4.                 Dim StartText As String = br.ReadChars(500)
    5.                 StartText = StartText.Replace(.ARTIST, .ARTIST.ToUpper)
    6.                 StartText = StartText.Replace(.SONGNAME, .SONGNAME.ToUpper)
    7.                 Dim mBytes() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(StartText)
    8.                 fs.Write(mBytes, 0, mBytes.Length - 1)
    Attached Images Attached Images  
    Last edited by |2eM!x; Dec 3rd, 2006 at 11:00 PM.

  8. #8
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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:
    1. Dim fs As New System.IO.FileStream("c:\test.mp3", IO.FileMode.OpenOrCreate)
    2.         Dim TestData() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes("TestData".ToCharArray)
    3.         fs.Seek(0, IO.SeekOrigin.Begin)
    4.         fs.Write(TestData, 0, TestData.Length)
    5.         fs.Close()
    I believe it overwrites whatever was there with whatever you write to it, with the FileStream.Write method...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width