|
By opening the file using the BinaryReader [^], and reading from it. You can use the ReadString [^] method to do so. Then compare the data that you read to the data that you're searching.
SimpleData wrote: I have searched on Google and MSDN but got nothing.
In what language did you query Google?
I'm sure there won't be a ready-to-go solution indeed, but you can put the different parts together to form a new solution
"please help in this regard. give the visual basic code for this as soon possible."
|
|
|
|
|
I've searched in English.
I tried doing it with ReadString but as I have said it didn't work.
Here is the code I was using:
FileStream fs = new FileStream("name.exe",FileMode.Open,FileAccess.ReadWrite);
BinaryReader br = new BinaryReader(fs);
string buff = String.Empty;
while(buff != "blablabla")
{
buff = br.ReadString();
if(buff == "blablabla")
Console.WriteLine("Found!");
}
I wrote this string to the end of file with BinaryWriter with this code.
FileStream fs = new FileStream("name.exe",FileMode.Open,FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(bw);
bw.Seek(0,SeekOrigin.End);
bw.Write("blablabla");
When I open the file with an hex editor I can see that my string is there but I can't find it with these codes.
modified on Saturday, July 11, 2009 2:55 PM
|
|
|
|
|
Try something like this;
FileStream fs = new FileStream("C:\\Windows\\System32\\calc.exe", FileMode.Open, FileAccess.ReadWrite);
BinaryReader br = new BinaryReader(fs);
string searchTerm = "manifestVersion";
byte[] file = new byte[fs.Length];
fs.Read(file, 0, (int)fs.Length);
string asText = Encoding.UTF8.GetString(file);
int index = asText.IndexOf(searchTerm);
Console.WriteLine("Found @position: " + index);
Console.WriteLine("'" + asText.Substring(index, searchTerm.Length) + "'");
Console.ReadKey();
"please help in this regard. give the visual basic code for this as soon possible."
modified on Saturday, July 11, 2009 5:23 PM
|
|
|
|
|
Hi,
I don't agree. Your code assumes all the file data is text; if it is, why use a BinaryReader at all? if it isn't, you need to interpret the binary data to locate the parts that are supposed to be strings, then treat those with code similar to yours, making sure to obey the string conventions in the file (length+content, content+NULL, whatever).
And if you don't know the file format, or just want to find everything that might be a string, then you must check for ASCII or Unicode characters explicitly, you can't just feed arbitrry binary data to an Encoder.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Luc Pattyn wrote: you must check for ASCII or Unicode characters explicitly
Run through the byte array, checking whether the byte is between 0 and 127, and if it is, convert it using Encoding.ASCII?
In retrospect, wouldn't it be faster to convert the searchterm and look in the array of bytes for that sequence?
"Do I give you even the slightest impression that I fear anything? Chuck Norris works for me." - JSOP[ ^]
|
|
|
|
|
Yes, if you know the string you want to find, converting it to bytes and then search bytes would be fastest. One would need to do this at least twice to accommodate for both ASCII and Unicode.
OTOH getting "all" the strings from a file works best the other way around: check a sequence of bytes for appropriate values (e.g. between 32 and 127, possibly alternating with zeros) and some mimimum length; doing it this way will miss all special characters, say the accented e, Euro/Pound sign, etc.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Ok, so it is obvious what the problem is.
Lets look at what you have:
1) an EXE file (binary, random data as far as BinaryReader is concerned).
2) add "blablabla" to the end.
3) a loop that does not end unless it suceeds in finding your string, or runs out of stream and throws an exception, that you aren't catching.
4) an equality test that is failing.
If I rewrote this as:
string s = @"hello there! This is what you are looking for: blablabla";
if(s == "blablabla")
{
Console.Writeline("Give the man a million bucks");
}
else
{
Console.WriteLine("Give the man a dead fish");
}
Do you think you could spot the problem yourself?
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
ReadString reads some kind of crappy length-prefixed string.. which means it can decide to read sometime like 2GB of data if you're lucky (nice EndOfStream exception).
I'd search for the first character, then see if the next character from the stream is the second character that you are searching, you can find the way from here.
|
|
|
|
|
Hi i want to have time in arraylist like following
9:45 AM
10:00 AM
10:15 AM
10:30 AM
....
so basically 15 mins are added to previous item..
How to do this?
Pls reply ASAP.
|
|
|
|
|
Member 3981366 wrote: Pls reply ASAP.
This is more likely to stop people from replying. Everybody here is a volunteer, if your problem is that urgent, then get your credit card out and go to rentacoder.com.
Firstly, try not to use ArrayList , use List<T> instead. It is type safe and more efficient. Look it up in the documentation for further details.
Something like:
List<DateTime> timeList = new List<DateTime>();
DateTime startTime = new DateTime(1, 1, 1, 9, 0, 0);
DateTime endTime = new DateTime(1, 1, 1, 18, 0, 0);
while (startTime <= endTime)
{
timeList.Add(startTime);
startTime.AddMinutes(15);
}
would work.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
To me, "ASAP" means the same as "when you can get around to it".
|
|
|
|
|
What will you do with it?
Depending on what you plan to do, you may not actually need to store all that in memory.
Here's an enumerator from my TimeRanger[^] article:
public static System.Collections.Generic.IEnumerable<System.DateTime>
MinuteEnumerator
(
System.DateTime StartPoint
,
System.DateTime EndPoint
,
ushort SteppingFactor
)
{
System.DateTime current = PIEBALD.Lib.LibTim.DateTruncate
(
StartPoint
,
PIEBALD.Lib.LibTim.TimeGranularity.Minute
) ;
if ( StartPoint <= EndPoint )
{
if ( current < StartPoint )
{
current = current.AddMinutes ( 1 ) ;
}
while ( current <= EndPoint )
{
yield return ( current ) ;
current = current.AddMinutes ( SteppingFactor ) ;
}
}
else
{
while ( current >= EndPoint )
{
yield return ( current ) ;
current = current.AddMinutes ( SteppingFactor*-1 ) ;
}
}
yield break ;
}
Or you could use this to load a List if you really want to store the information.
|
|
|
|
|
how to use ascii code??
i want when i press enter key my button is click.
how i do this???
|
|
|
|
|
Firstly, read the bit at the top of this list, where is says "How to get an answer to your question".
"Hi.." is not a description of your problem. A description that relates to the problem encourages people who think "I might know the answer to that" to look, and maybe, to answer...
Secondly, I know English is not your first language, so I am guessing that what you want is:
You have a form of some kind (lets call it Form1), which has a button (lets call it Button1).
When you press the Enter key, you want the buttons click event to be fired.
If so, then: Click on Form1 in the design view, and look at the properties.
Find the "Accept Button" property.
Use the drop down list to select "Button1"
Build your project.
Enter will now activate the Button1 Click event.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
Jeepers. Have you thought about buying a book ? Have you read my article on how to use google ?
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
If I can't use Google, how am I supposed to find your article...?
|
|
|
|
|
If they have the ability to use a forum to post these messages, they shouldn't have any problems using Google.
|
|
|
|
|
Hi there,
I have a StreamEvent from a StockExchange Data Provider API. The Event is triggered every time a new price occurs for the respective Instrument on the Exchange.
I can fill the Stream with multiple Intruments, lets say 50.000. So the StreamEvent is triggered about mutiple thousand times in one second.
In the Stream, I first check the price difference in percent between the last price and the new one:
private double abw;
void TPBrief_Brief(int SymbolNr, float NewPrice, float Volume, DateTime Time)
{
abw = (((InstrumentObject)os[SymbolNr]).LastPrice - NewPrice) / (((InstrumentObject)os[SymbolNr]).LastPrice/ 100);
if (abw >= 1.5) {
}
As you can see, I hold my Instrument Objects in a Hashtable, key is Symbolnr, value the InstrumentObject. The first line in this event is the one which is triggered that often, so I just want to know how to optimize this line.
As I learned from various articles, Hashtable should be replaced with a Dictionary, this is one thing I will do.
But what about this variation:
void TPBrief_Brief(int SymbolNr, float NewPrice, float Volume, DateTime Time)
{
IntrumentObject io = ((InstrumentObject)os[SymbolNr]);
abw = (io.LastPrice - NewPrice) / (io.LastPrice/ 100);
if (abw >= 1.5) {
}
Could this be faster than the first one? What's about the Calculation of the difference? Can this code fragment be optimized?
Any help would be appreciated!
|
|
|
|
|
As far as calculating the difference is concerned, may I suggest that you google, bing or otherwise search on performance measurement c#, as there are so many hits that it is difficult to recommend just one.
As far as optimizing the code, the only real way to tell is to measure the performance of as many variations as you can think of. I am not able to see anything obvious, although I am pretty sure that others will offer an opinion.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
I doubt this calculation is a bottleneck. Have you done any performance profiling? How much throughput are you getting at the moment? How much throughput are you getting with this handling code removed?
Anyway, a bit of maths gives:
abw = NewPrice / io.LastPrice;
if ( abw >= 1.015 || abw <= 0.985 ) {
Also, if the SymbolNr values are sequential ( or at least small ), you could use an array of InstrumentObject with SymbolNr as the index.
Nick
----------------------------------
Be excellent to each other
|
|
|
|
|
Thank you for your input. The Calculation of the Percentage is a good one
I tested now the calculation with this different algos:
private void StreamEventHash(int SymbolNr, float NewPrice, float Volume, DateTime Zeit_kurs)
{
abw = (((OSObject)osHash[SymbolNr]).LastPrice - NewPrice) / (((OSObject)osHash[SymbolNr]).LastPrice / 100);
if (abw >= 5)
{
}
}
private void StreamEventHashNewObject(int SymbolNr, float Kurs, float Volume, DateTime Zeit_kurs)
{
OSObject oo = ((OSObject)osHash[SymbolNr]);
abw = (oo.LastPrice - NewPrice) / (oo.LastPrice / 100);
if (abw >= 5)
{
}
}
private void StreamEventHashNewObjectAs(int SymbolNr, float Kurs, float Volume, DateTime Zeit_kurs)
{
OSObject oo = osHash[SymbolNr] as OSObject;
abw = (oo.LastPrice - NewPrice) / (oo.LastPrice / 100);
if (abw >= 5)
{
}
}
I use the stopwatch class for this. I found out that the first one is the slowest So there is performance optimization possible.
But I can't tell which one of the other 2 algos are faster, because when I change the order the voids are tested, different results occur. It seems that the one which is tested first is always a little bit slower. I am about to figure out how to get reliable results, maybe to run the tests multiple times in random order.
And than I have to find out what your proposed algo
abw = NewPrice / io.LastPrice;
if ( abw >= 1.015 || abw <= 0.985 ) {
will do
modified on Saturday, July 11, 2009 8:31 AM
|
|
|
|
|
I'd try multiplying by 1/100 instead of dividing by 100, the optimizer is not exactly smart but it will always collapse constants such as "1/100" - and seeing as an fdiv is like 15 times (depends on the divisor) as slow as an fmul and not even pipelined, the difference could be huge.
|
|
|
|
|
Thank you very much for your input.
Anyway, I found out that 1,000,000 triggers only take appr. 500 ms. So this can't be my bottleneck
modified on Saturday, July 11, 2009 10:30 AM
|
|
|
|
|
Oh, I don't know that
That's not exactly useful for you to know, but if I hadn't said it you might be waiting for my reply..
I hope someone else here knows more about that kind of thing
|
|
|
|
|
I found out that 1,000,000 triggers only take appr. 500 ms. So this can't be my bottleneck Frown
It seems that the bottleneck is on the DataProvider APIs side. If I put more than 50.000 instruments in the datastream, then somehow sometimes the events aren't triggered anymore. This is my main problem.
BUT: I now that this has somehow also to do with CPU power. This 'disconnects' happen much more often on systems with slower CPU's. So I think there should be a possibility to improve my App - but I don't know where. I already tried to raise the priority of the DataProvider Client Process - no result.
It seemes that somewhere in the system, if the traffic is high enough, there is a traffic jam causing the strange behaviour. Should I change something in the Network Preferences? But what?
|
|
|
|
|