How to Search for a Piece of Text in Binary File by Using fstream - C/C++?
I got a binary file, i opened the file. I need to look for this "255.255.255.255", and then replace it with the user newly inserted IP...
I know there is something called seekp(), but it needs me to determine where to point in the file. I do not know yet. Once i find the spot 255.255... I will have to write from the iso::beg, right?
1. I can go through the file line by line by using getline()...Is there another solution?
2. Assume i went through the file & i found 255.255.255.255, to overwrite it do i t have to start from 2, or before 2? I guess ios::beg has to be involved in someway.
3.
Code:
ofstream file1;
file1.open("OutputTrial1.txt", ios::binary );
file1.write(temp,sizeof(temp));
file1.close();
In this line file1.open("OutputTrial1.txt", ios::binary ); OutputTrial1.txt is not binary, so how come we specified it as a binary in parameter two?
Regards
Re: How to Search for a Piece of Text in Binary File by Using fstream - C/C++?
Unless it's a really huge file, it's probably easiest to just read the entire file contents into a single string using getline() with EOF as the delimiter, then do the replace, then write the whole thing back out.
If it *is* a really huge file, reading byte by byte until you find a '2' and then special-casing from there might be the easiest way. (At the furthest extent of this approach, you'd essentially have the DFA-based substring matching algorithm.)
Re: How to Search for a Piece of Text in Binary File by Using fstream - C/C++?
Quote:
Originally Posted by
Lindley
Unless it's a really huge file, it's probably easiest to just read the entire file contents into a single string using getline() with EOF as the delimiter, then do the replace, then write the whole thing back out.
If it *is* a really huge file, reading byte by byte until you find a '2' and then special-casing from there might be the easiest way. (At the furthest extent of this approach, you'd essentially have the DFA-based substring matching algorithm.)
The best may be a mixture of both of them. Read a fixed size chunk of data, replace the relevant occurences and read the next chunk. ThereĀ“s a special case that must be handled when the text exceeds one chunk and is continued in the next one.
Re: How to Search for a Piece of Text in Binary File by Using fstream - C/C++?
1. Here is what i did! I do not know how good this piece of code is, it works %90 i need to fix it little bit, but the main disadvantage is i have to use Hex Workshop to get the offset of ip & offset of port then pass it manually to the parameter. I find it inefficient thats why i though to do it through fstream since its more flexible. I want to locate IP then overwrite it, same thing with port. Please look at the code & give suggestions if you don't mind
Code:
//get offsets through parameters and data to be inserted in place offset
editBinary(long offsetOfIP, long offsetOfPortNumber, const char *IPAddress, const char *portNumber)
{
const DWORD MAX_NUMBER_OF_BYTES_IN_IP = 15;
const DWORD MAX_NUMBER_OF_BYTES_IN_PORT = 5;
DWORD numOfBytesWritten = 0;
HANDLE hFile = CreateFile(L"application.exe", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile == INVALID_HANDLE_VALUE){
cout<<"CreateFile() error: "<<GetLastError()<<endl;
cout<<"*Make sure the file app.exe exists in the folder."<<endl;
cout<<"Please press any key to go back"<<endl;
cin.get();
//get back
}
//point to IP offset
DWORD successResult = SetFilePointer(hFile, offsetOfIP, NULL, FILE_CURRENT);
if(successResult == INVALID_SET_FILE_POINTER)
{
cout<<"SetFilePointer() error: "<<GetLastError()<<endl;
cout<<"Please press any key to go back"<<endl;
cin.get();
//get back
}
//overwrite the old IP - IPAddress contains the new IP
BOOL numberOfIPBytesWrittenToFile = WriteFile(hFile, IPAddress, MAX_NUMBER_OF_BYTES_IN_IP, &numOfBytesWritten, NULL);
if(numberOfIPBytesWrittenToFile == 0)
{
cout<<"WriteFile() error: "<<GetLastError()<<endl;
cout<<"Please press any key to go back"<<endl;
cin.get();
//get back
}
//point to where port is located
successResult = SetFilePointer(hFile, offsetOfPortNumber, NULL, FILE_BEGIN);
if(successResult == INVALID_SET_FILE_POINTER)
{
cout<<"SetFilePointer() error: "<<GetLastError()<<endl;
cout<<"Please press any key to go back"<<endl;
cin.get();
//get back
}
//overwrite the old port number - portNumber contains the new port number
numOfBytesWritten = 0;
BOOL numberOfPortBytesWrittenToFile = WriteFile(hFile, portNumber, MAX_NUMBER_OF_BYTES_IN_PORT, &numOfBytesWritten, NULL);
if(numberOfPortBytesWrittenToFile == 0)
{
cout<<"WriteFile() error: "<<GetLastError()<<endl;
cout<<"Please press any key to go back"<<endl;
cin.get();
//get back
}
//close file
BOOL isFileClosed = CloseHandle(hFile);
if(isFileClosed == 0)
{
cout<<"CloseHandle() error: "<<GetLastError()<<endl;
cout<<"Please press any key to go back"<<endl;
cin.get();
//get back
}
cout<<"Editing Succeeded."<<endl;
}
2.
Quote:
then do the replace, then write the whole thing back out.
All data resides in the other file i just need to go through it. Once i find the spot i want to change, i start writing from there until data get fully replaced, then close the file. Can you clarify about why do i have to write the whole thing back out - this is right if i moved everything in a temp file afaik.Correct me if i'm wrong
Ignore boundary checking for now...