473,769 Members | 5,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Replacing Strings Binary

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(mybinfil e, "rb");
contents = f.read();
contents.replac e(oldbinstring, newbinstring);
f.close();

f.open(mybinfil e, "wb");
f.write(content s);
f.close();

Thanks for any help :D

Sam

Jul 23 '05 #1
3 1808
Son of Sam wrote:
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(mybinfil e, "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.replac e(oldbinstring, newbinstring);
f.close();

f.open(mybinfil e, "wb");
f.write(content s);
Again, 'write' doesn't take a single argument. RTFM, will you?
f.close();


V
Jul 23 '05 #2
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_repl ace()
function/class:

string::size_ty pe p;

while( p = contents.find(o ldbinstring) != string::npos)
{
contents.replac e( p, strlen(oldbinst ring.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.replac e_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.

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
7400
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently interchangeable in all circumstances (as long as they're paired) so there's no overloading to muddy the language. Of course there could be some interesting problems with current code that doesn't make a distinction, but it would be dead easy to fix...
13
15257
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the pattern can occur on any byte boundary in the file, so chunking through the code at 16 bytes a frame maybe a problem. The file itself isn't so large, maybe 32 kbytes is all and the need for speed is not so great, but the need for accuracy in the...
5
5850
by: Robert Manea | last post by:
Hello everyone, I wrote, simply as an exercise, a small piece of code to find 'strings' (defined as an amount of at least 3 ASCII characters followed by a non ASCII character) in binary files. The purpose of the program is to serve as a facile 'strings' (Unix command) replacement and to be 100% ANSI C. Unfortunatelly it operates notedly slower than the original 'strings' from the fileutils package.
2
3413
by: Christopher Beltran | last post by:
I am currently trying to replace certain strings, not single characters, with other strings inside a word document which is then sent to a browser as a binary file. Right now, I read in the word file, convert the FileStream into a string using Unicode encoding, then do a replace, then convert the string back to a byte using Unicode encoding which i then Response.WriteBinary(bytes) to the browser. This works fine although the actual...
12
2604
by: anonymous | last post by:
Hello, I need to replace this char  with another char. However I am not able to acieve this. I tried this but it doesnt work: str = str.Replace(chr(asc(194)), "") Can somebody help ?
12
5928
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: <gibberish>file//g:\pathtofile1<gibberish>file//g:\pathtofile2<gibberish> etc. I want to remove the "g:\" from the file paths. I wrote a console app that successfully reads the file and writes a duplicate of it, but fails for some reason to do the "replacing" of the "g:\". The code...
14
1801
by: Josh Baltzell | last post by:
I am having a lot more trouble with this than I thought I would. Here is what I want to do in pseudocode. Open c:\some.pdf Replace "Replace this" with "Replaced!" Save c:\some_edited.pdf I can do this in notepad and it works fine, but when I start getting in to reading the files I think it has some encoding problem. I tried saving the file with every encoding option. When I open a PDF in the
4
455
by: CoreyWhite | last post by:
/* WORKING WITH STRINGS IN C++ IS THE BEST WAY TO LEARN THE LANGUAGE AND TRANSITION FROM C. C++ HAS MANY NEW FEATURES THAT WORK TOGETHER AND WHEN YOU SEE THEM DOING THE IMPOSSIBLE AND MAKING COMPACT COHERENT CODE THAT WORKS WITH STRINGS, IT ALL BEGINS TO MAKE SINCE*/ /* The basics of C++ are Classes, that build Types. Which are used to create quick and dirty routines in the smallest possible space. The Classes & Routines uses the...
1
4082
by: duncanm817 | last post by:
hi, I require some assistance in understanding how to accomplish replacing a particular hex value in a binary file on aix. in a binary file I need to find the following combination: 5A 00 10 D3 A9 XX XX XX XX XX XX XX XX XX XX XX XX 0C 5A XX represents any valid HEX value then replace this with the following HEX value
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.