Question:
How to Search for a Piece of Text in Binary File by Using fstream - C/C++ - Re-explaination Needed?
Michael Jackson
2009-02-23 18:13:55 UTC
I got a binary file, i opened the file. I need to look for this ASCII "255.255.255.255" which of type const char *, and then replace it with the user newly inserted any data, in this case IP.

I find this solution simple & interesting:

I'll need to find the following sequence of bytes (hex): 32 35 35 2E 32 35 35 2E 32 35 35 2E 32 35 35. Read the file byte-by-byte, and try to match the pattern. If/when you do find that sequence, you'll write out the new IP address starting at the location you found the first '2'.

Rather than trying to overwrite the original file, it's probably easier to create a new file with the altered sequence of bytes. You should be able to do it in one pass, and you won't need to read the entire file into memory. Maybe just a buffer of 15 bytes, a sliding window that you move through your input file, would do the trick.

********************************************************************************
************

QUESTION:

>>You should be able to do it in one pass, and you won't need to read the entire file into memory. Maybe just a buffer of 15 bytes, a sliding window that you move through your input file, would do the trick

Can you re-explain the above part to me please? - A sliding window that you move through your input file, would do the trick, does sliding window here means reading data in fragments or this a special term for developers? - I guess its my lack of English sometimes puts barrier on me...
Three answers:
Pat M
2009-02-23 19:31:23 UTC
In other words, you read in the file one byte at a time. So first you read in the first 15 bytes. If that range (bytes 0-14) doesn't contain the given sequence, you need to check if bytes 1-15 does. Since you already have bytes 0-14 in a buffer, all you need to do is throw out byte 0 (you already know it doesn't match the pattern) and read in byte 15. By doing this you minimize the work you have to do by keeping the bytes you need until they aren't of use to you anymore.



Did that answer you question?
2009-02-23 20:26:59 UTC
Or you can read the entire file into a char variable, find the location of "255.255.255.255", write the variable out to the new file up to the buye before the first 255, write the IP, then start writing the rest of the variable, starting after the last 255.
?
2016-10-25 17:28:40 UTC
wise and powerful, examining a fashionable disaster.. even inspite of the undeniable fact that the disaster is nicely-known and the roots of it implanted many years in the past, contained in the fertile soil of overconsumption and indiscreet mind-washing-commercial... no human being escaped from it. actually one statement: Paradigm is a noun (social gathering). the wonderful word up there is "paradigmatic". in the different case ideal! So that's both 'Paradigmatic Codes" or "Coded Paradigms"


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...