473,324 Members | 2,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Opening a file as binary and editing it

abdoelmasry
104 100+
Hi All
Please any one can help me to complete my code !
My code is about opening file as binary and edit it

#include <iostream>
#include <iostream>
using namespace std;

int main(){
fstream nfile;
nfile.open("new.txt",ios::in|ios::out|ios::binary) ;
nfile.seekp(2);
nfile<<"code";
return 0;
}

my main problem is:
I need to insert my text in file after the position that i have set pointer on it
but
without replacing any data from file
Ex:.

if the file have string:
EGYHK.COM

i want this text be (EGcodeYHK.COM)
but when i run my code.. it's delete any characters after starting pointer place
i got it:
EGcodeCOM
not
EGcodeYHK.COM

sorry im not so good in english
i hope that i explained my mean
I Want To Put String Not Write it above the existing string in the file

Thank You
Oct 24 '06 #1
4 2382
vinothg
21
When you open the binary file add ios::ate so that the string will get appended instead of overwriting i.e

nfile.open("new.txt",ios::in|ios::out|ios::ate|ios ::binary);

Hope it will work !!!
Oct 24 '06 #2
abdoelmasry
104 100+
Thank You vinothg for Help
But
this code will add my code at the end of file
i need to add it at the before the first line of file

-i have this string in a file:(is my love)

-i need to add string (programming) to the file

-your code using ios::ate will make string in my file: (is my love programming)

-i want it : (programming is my love)


Thanks
Oct 25 '06 #3
vinothg
21
Ok if you know the exact offset where you want to append then this piece of code may help you.

#include <iostream>
#include <fstream>
using namespace std;

int main(){
ofstream os("test",ios::out|ios::app|ios::binary);
os.seekg(20,ios::beg);
char buffer[3] = {"Hi"}
os.write((char*)(&buffer),sizeof(buffer));
os.close();
}
Oct 26 '06 #4
abdoelmasry
104 100+
i found this code in many forums
i think it's right but it's not working with me
i always found a strange characters in test file
Oct 27 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Alfons | last post by:
Hello, I have build a program that can do file transferring between a Windows XP computer and a DOS computer via a serial port. The Windows program I have build in C++ with Visual Studio 6.0....
4
by: comp.lang.php | last post by:
I borrowed the following function from the PHP manual user notes: if (!function_exists('is_binary')) { /** * Determine if a file is binary. Useful for doing file content editing * *...
11
by: moony marouane | last post by:
Hi all I'm looking for code regarding opening (not saving) binary files (pdf, word, excel...) from hard disk with asp, the website is hosted in C:\ and the pdf files are in D:\. Does anyone...
2
by: ritesh | last post by:
Hi, I'm facing a problem in which I need to edit an already created file, and the editing needs to be done at the start of the file rather then appending to the file. OS - Linux,Solaris ...
13
by: James Reid | last post by:
I'm looking for the source code for a DOS command line c/c++ program that can change file associations. In other words it can cause any version of Windows to associate a particular .exe file with a...
2
abdoelmasry
by: abdoelmasry | last post by:
hi any one can help me plz ?? my code is about opening file as binary then get data from file to edit and rewrite data to file this is the code: #include <cstdlib> #include <iostream>...
11
by: rvr | last post by:
Would someone mind showing me how to strip the first byte from a binary file? For some reason I can't figure this out from the binary file editing examples I've read. Thanks. ~rvr
6
by: Alexander Vasilevsky | last post by:
How do I know whether the file is text or binary? http://www.alvas.net - Audio tools for C# and VB.Net developers + Christmas discount
1
by: manu | last post by:
Hi, Can anyone please tell me the difference of opening file in Binary mode or in Text mode?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.