|
-
Jun 3rd, 2004, 05:23 PM
#1
Thread Starter
Frenzied Member
Find and replace a text string in a binary file
I have code to read the file and print it out. I need to search for a string and replace it so I figured it would be better to do it in C++.
Code:
unsigned char ch;
int linecounter=0;
FILE *in;
in = fopen("myfile.bin","rb");
if(ferror(in) ){
perror("Error opening input file:");
exit(1);
}
while (!feof(in) ){
if( fread(&ch,sizeof(ch),1,in) ==1){
/* do stuff with the character you just read */
printf("%c",ch);
linecounter++;
if ( !(linecounter%30) )
printf("\n");
}
}
fclose(in);
Can this be done in C++ to get the data in a string?
Last edited by wey97; Nov 10th, 2004 at 09:43 AM.
-
Jun 4th, 2004, 03:18 AM
#2
It can be done, but I recommend you use the file streams and strings to do that. Read a bit about the C++ standard library.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 4th, 2004, 08:53 AM
#3
Thread Starter
Frenzied Member
I have it in a buffer now but null characters are causing trouble.
Code:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ifstream ifs("C:\\test.lnk", ios::in | ios::binary);
char buffer[1024];
ifs.read(buffer, 1024);
for(int i = 0; i < 1024; i++)
{
cout << buffer[i];
}
}
That prints everything but if I do
string str = buffer;
cout << str << endl;
cout << str.length() << endl;
then obviously I only get one character printed and str.length() is 1
-
Jun 4th, 2004, 09:20 AM
#4
Well, strings are for character data, not binary data. The file seems to contain binary data, no? Or why does it contain so many NULs, why do you open it as binary?
You can use a vector or a deque for binary data.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 4th, 2004, 09:25 AM
#5
Thread Starter
Frenzied Member
Well it's a shortcut (.lnk) file and there is a path string nested in it something like 'C : \ f o l d e r \' etc.
where the spaces are nulls. I just put it in a char buffer so I could visually see what I am looking to replace and hopefully search it as string.
-
Jun 4th, 2004, 09:34 AM
#6
Hmmm...
I think you should read it all into a binary buffer, then parse the binary data to find the place where the string is. Then check only that string. lnk files have a structured format, try wotsit.org. Search for lnk there and look at the document in the middle link.
Last edited by CornedBee; Jun 4th, 2004 at 09:39 AM.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 4th, 2004, 10:37 AM
#7
Thread Starter
Frenzied Member
The link was helpful.
Here's the code if anybody needs it.
Code:
#include <windows.h>
#include <shobjidl.h>
#include <shlguid.h>
#include <iostream>
#include <string>
using namespace std;
typedef unsigned int uint;
bool StringFind(string sSearchIn, string sSearchFor, uint &nPos)
{
uint nTemp = (uint)sSearchIn.find(sSearchFor);
if(nTemp < sSearchIn.length())
{
nPos = nTemp;
return true;
}
return false;
}
bool Replace(string &sLine, string sReplaceWhat, string sReplaceWith)
{
uint nPos = 0;
uint i = 0;
while(StringFind(sLine, sReplaceWhat, nPos))
{
sLine = sLine.replace(nPos, sReplaceWhat.length(), sReplaceWith);
i++;
}
if(i)return true;
return false;
}
void ModArguments(string sFileLink, string sFind, string sReplace)
{
HRESULT hres;
IShellLink *psl;
IPersistFile *ppf;
WCHAR szTextW[1024];
TCHAR szText[1024];
string str;
CoInitialize(NULL);
// Get a pointer to the IShellLink interface.
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (LPVOID*)&psl);
MultiByteToWideChar(0, 0, sFileLink.c_str(), -1, szTextW, 1024);
psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
ppf->Load(szTextW, STGM_READ);
psl->GetArguments(szText, 1024);
str = szText;
Replace(str, sFind, sReplace);
psl->SetArguments(str.c_str());
ppf->Save(szTextW, TRUE);
ppf->Release();
psl->Release();
CoUninitialize();
}
int main()
{
ModArguments("C:\\test.lnk", "OldArgs", "NewArgs");
system("pause");
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|