Hi, I am making a Binary Reader find then read a string and input it into a textbox. I have 2 questions. I need to search for a string and read the string a byte afterwards. The string I am reading can be 1 - 4 bytes long. It is offset between "". (Two quotes [it is inside])
1) Is there a way I can make it find a string faster because right now on large files it just locks up.
2) What is wrong with this code, I keep catching these errors.
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "All Files|*.*";
ofd.Title = "Open Profile";
ofd.FileName = "";
if (ofd.ShowDialog() == DialogResult.OK)
fileLocation = ofd.FileName;
//Searches for the clan tag
string search = "clanName ";
BinaryReader br = new BinaryReader(new FileStream(fileLocation, FileMode.Open, FileAccess.Read));
// Starting offset
br.BaseStream.Position = 0x14000;
string found = "";
try
{
// read next 65536 chars
found = new string(br.ReadChars(0x10000));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
// Find our starting position in buffer
int index = found.IndexOf(search);
textBox1.Text = found.Substring(index, 20);
}
Loading
Jan MontanoPosted Jan 5, 2009, 7:44 PM
Here's to get you started.
private static void BRead()
{
FileStream fileStream = File.OpenRead(@"C:\test.txt");
BinaryReader binaryReader = new BinaryReader(fileStream);
string searchKey = "test";
string data;
int searchIndex = 0;
int currentPosition = 0;
while (currentPosition <= fileStream.Length)
{
data = new string(binaryReader.ReadChars(0x10000));
currentPosition += 0x10000;
searchIndex = data.IndexOf(searchKey);
if (searchIndex >= 0)
{
Console.WriteLine(searchIndex);
Console.WriteLine(data.Substring(searchIndex-3,2));
break;
}
}
}
*Note: Keep in mind that with the code above, you won't be able to read the searchKey if it the searchKey starts right before the 65536th byte and ends after the 65536th byte. You have to tweak this a little bit.
chrisPosted Jan 5, 2009, 3:54 PM
Jan MontanoPosted Jan 4, 2009, 8:42 PM
Are you required to use a BinaryReader?
Here's an example I found using a regular expression and a StreamReader instead.
just user the regular expression