Find and Replace in Binary File

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mouac01@yahoo.com

    Find and Replace in Binary File

    Newbie here. How do I do a find and replace in a binary file? I need
    to read in a binary file then replace a string "ABC" with another
    string "XYZ" then write to a new file. Find string is the same length
    as Replace string. Here's what I have so far. I spent many hours
    googling for sample code but couldn't find much. Thanks...

    public static void FindReplace(str ing OldFile, string NewFile)
    {
    string sFind = "ABC"; //I probably need to convert these
    to a byte array
    string sReplace = "XYZ"; //but I don't know how.
    int i;
    FileStream fin = new FileStream(OldF ile, FileMode.Open);
    FileStream fout = new FileStream(NewF ile,
    FileMode.Create );
    do
    {
    i = fin.ReadByte();
    if (i != -1)
    {
    //I think I need to compare the byte being read in
    //to the 1st sFind byte array here. If it matches
    then
    //store the position and compare the next byte.
    //If all 3 bytes match then replace with sReplace
    byte array.
    //I'm just not sure how to do it.
    fout.WriteByte( (byte)i);
    }
    } while (i != -1);
    fin.Close();
    fout.Close();
    }
  • Mufaka

    #2
    Re: Find and Replace in Binary File

    I have done this before. The approach I took was to read the file bytes
    into memory (because the files I was working with were small), move the
    file out of the way, and then write a new file replacing the "find"
    bytes with the "replace" bytes. So similar to your approach.

    Here's a snippet from what I wrote (it was in 1.1 and was just "quick
    and dirty" code):

    ArrayList newBytes = new ArrayList(bytes .Length);
    int ndx = 0;

    for (int x = 0; x < bytes.Length; x++) // bytes is the original files bytes
    {
    if (bytes[x] == findBytes[ndx]) // findBytes is a byte[] from the
    "find" string
    {
    if (ndx == findBytes.Lengt h - 1)
    {
    for (int y = 0; y < replaceBytes.Le ngth; y++) //
    replaceBytes is a byte[] from the "replace" string
    {
    newBytes.Add(re placeBytes[y]);
    }
    ndx = 0;
    }
    else
    {
    ndx++;
    }
    }
    else
    {
    if (ndx 0)
    {
    for (int y = 0; y < ndx; y++)
    {
    newBytes.Add(fi ndBytes[y]);
    }
    }
    ndx = 0;
    newBytes.Add(by tes[x]);
    }
    }

    ndx is used to keep track of which byte in the findBytes should be
    compared to the original files byte.

    mouac01@yahoo.c om wrote:
    Newbie here. How do I do a find and replace in a binary file? I need
    to read in a binary file then replace a string "ABC" with another
    string "XYZ" then write to a new file. Find string is the same length
    as Replace string. Here's what I have so far. I spent many hours
    googling for sample code but couldn't find much. Thanks...
    >
    public static void FindReplace(str ing OldFile, string NewFile)
    {
    string sFind = "ABC"; //I probably need to convert these
    to a byte array
    string sReplace = "XYZ"; //but I don't know how.
    int i;
    FileStream fin = new FileStream(OldF ile, FileMode.Open);
    FileStream fout = new FileStream(NewF ile,
    FileMode.Create );
    do
    {
    i = fin.ReadByte();
    if (i != -1)
    {
    //I think I need to compare the byte being read in
    //to the 1st sFind byte array here. If it matches
    then
    //store the position and compare the next byte.
    //If all 3 bytes match then replace with sReplace
    byte array.
    //I'm just not sure how to do it.
    fout.WriteByte( (byte)i);
    }
    } while (i != -1);
    fin.Close();
    fout.Close();
    }

    Comment

    • mouac01@yahoo.com

      #3
      Re: Find and Replace in Binary File

      Thanks for all your comments. Here's what I'm trying to mimic in
      VB6. I guess .NET is more restrictive in what type of file you can do
      find/replace. Sounds too difficult in C# so I might just have to
      revert back to VB6 for this one. Thanks...

      Sub FindRplace()
      Dim sBuffer As String
      Dim ff As Integer
      ff = FreeFile
      Open txtFile.Text For Binary As #ff
      sBuffer = Space$(LOF(ff))
      Get #ff, 1, sBuffer
      Close #ff
      sBuffer = Replace(sBuffer , "ABC", "XYZ")
      Open txtNew.Text For Binary As #ff
      Put #ff, , sBuffer
      Close #ff
      End Sub

      Comment

      • Peter Duniho

        #4
        Re: Find and Replace in Binary File

        On Mon, 25 Feb 2008 11:56:50 -0800, <mouac01@yahoo. comwrote:
        Thanks for all your comments. Here's what I'm trying to mimic in
        VB6. I guess .NET is more restrictive in what type of file you can do
        find/replace. Sounds too difficult in C# so I might just have to
        revert back to VB6 for this one.
        It's not difficult in C#. The code would not even be significantly
        different from the VB6 code you posted, other than the lack of a
        short-hand "replace" feature for dealing with byte arrays (which you
        should be able to easily write yourself). And your VB6 code has all the
        same problems we've warned that could come up doing it in C#.

        But if you've already got VB6 code to do what you want, maybe you should
        just stick with that if you'd rather not use the suggestions offered here.

        Pete

        Comment

        Working...