Replacing Strings Binary

S

Son of Sam

Hi, I just want to open a file, let it replace 1 string (which occurs a
few times) and write it to the same file, it must be binary.

I tryed following code but it doesnt work as I thought:

#include <iostream>
#include <fstream>
#include <string>
#include <tchar.h>
#include <stdio.h>
#include <time.h>
#include <ctime>
#include <cstdlib>
#include <sstream>
#include <vector>

using namespace std;

fstream f;
string contents;
string oldbinstring = "1234567";
string newbinstring = "a610f82";
string mybinfile = "boom.bin";


f.open(mybinfile, "rb");
contents = f.read();
contents.replace(oldbinstring, newbinstring);
f.close();

f.open(mybinfile, "wb");
f.write(contents);
f.close();

Thanks for any help :D

Sam
 
V

Victor Bazarov

Son said:
Hi, I just want to open a file, let it replace 1 string (which occurs
a few times) and write it to the same file, it must be binary.

I tryed following code but it doesnt work as I thought:

#include <iostream>
#include <fstream>
#include <string>
#include <tchar.h>
#include <stdio.h>
#include <time.h>
#include <ctime>
#include <cstdlib>
#include <sstream>
#include <vector>

using namespace std;

Is there like a function or something inside which the following
code is located?
fstream f;
string contents;
string oldbinstring = "1234567";
string newbinstring = "a610f82";
string mybinfile = "boom.bin";


f.open(mybinfile, "rb");
contents = f.read();

There is no member function 'read' that takes no arguments. You
are probably thinking of 'getline' or something like that. However,
it won't help you if the file is binary. You need to find the size
of the file, declare a buffer of that size and read the entire file
into that buffer.
contents.replace(oldbinstring, newbinstring);
f.close();

f.open(mybinfile, "wb");
f.write(contents);

Again, 'write' doesn't take a single argument. RTFM, will you?
f.close();

V
 
S

Son of Sam

Ok nevermind I got that all now I got the file as a string now in a
variable called contents, now I want to write a search_and_replace()
function/class:

string::size_type p;

while( p = contents.find(oldbinstring) != string::npos)
{
contents.replace( p, strlen(oldbinstring.c_str()), newbinstring);
} // this doesnt work

So this is what I tryed, at the end I want that all strings
oldbinstring in the string contents will be replaced by the new string
newbinstring.

Best would be a function or class so i can use it like this:
contents.replace_string(string oldbinstring, string newbinstring);

Or so i can call it as function:
contents = replace_string(string contents, string oldbinstring, string newbinstring);

Maybe anyone can help me out with that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top