I need to be able to search a binary file for a string of hexadecimal characters and then prompt the user so they can decide if they want to change it. If they enter a "y" then the program changes the string and prints the change to the new file, if they enter a "n" then the program moves on to the next match. The goal is to create an identical file except where the program has replaced the search string with 00s. You can download an example of a file I would be modifying from:

http://members.lycos.co.uk/jfulley/example.bin

You're going to have to copy and paste the location into your browser since it's hosted on a free website.

The program below stops responding after it finds the first string match and prompts the user for a response. As soon as you enter a y or n the program stops responding.

$original = &promptUser("Enter original filename"); $modified = &promptUser("Enter modified filename"); open($ORIG, "$original"); binmode($ORIG); open($MOD, ">$modified"); binmode($MOD); $/ = "\x00"; while (<$ORIG>) { $count = (($change = $_) =~ s/(\x33\x33\x39\x39.*?\x00)/"\x00" x lengt +h($1)/e); if ($count == null) { print $MOD $_; } else { $answer = &promptUser("Modify $1?"); if ($answer == "y") { print $MOD $change; } else { print $MOD $_; } } } close $MOD; close $ORIG; sub promptUser { local($promptString,$defaultValue) = @_; if ($defaultValue) { print $promptString, "[", $defaultValue, "]: "; } else { print $promptString, ": "; } $| = 1; # force a flush after print $_ = <STDIN>; # get the input from STDIN chomp; if ("$defaultValue") { return $_ ? $_ : $defaultValue; # return $_ if it has a value } else { return $_; } }

In reply to perl command line prompts by pccode

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.