473,498 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Opening, reading and writing binary data to file

abdoelmasry
104 New Member
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>
#include<fstream>
#include<string>
#include<conio.h>
using namespace std;
int main()
{
int i=1,add=0;
string *marry=new string[i];
fstream file;
int spos=0,filepos1=0,change=0;
string mline,newline;
file.open("test.txt",ios:: out|ios::in|ios::binary);
while(!file.eof()){
filepos1=file.tellp();
//get line from file
getline(file,mline);
//copy new line from main line to edit
newline=mline;
//edit the line as string(replace VB with C++)
spos=newline.find("vb");
while(spos!=string::npos){
newline.replace(spos,2,"c++");
spos=newline.find("vb");
}
//Inject Lines to Memory In Dynamic Array
marry[add]=newline;
cout<<marry[add]<<endl;
add++;
i++;
}
file.close();
//Open the file to rewrite data from memory to it
file.open("test.txt",ios:: out|ios::in|ios::binary|ios::trunc);
for(int j=0;j<add;j++){
file<<marry[j]<<endl;
}
file.close();
return 0;
}

Thanks
Nov 10 '06 #1
2 4804
vpawizard
66 New Member
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>
#include<fstream>
#include<string>
#include<conio.h>
using namespace std;
int main()
{
int i=1,add=0;
string *marry=new string[i];
fstream file;
int spos=0,filepos1=0,change=0;
string mline,newline;
file.open("test.txt",ios:: out|ios::in|ios::binary);
while(!file.eof()){
filepos1=file.tellp();
//get line from file
getline(file,mline);
//copy new line from main line to edit
newline=mline;
//edit the line as string(replace VB with C++)
spos=newline.find("vb");
while(spos!=string::npos){
newline.replace(spos,2,"c++");
spos=newline.find("vb");
}
//Inject Lines to Memory In Dynamic Array
marry[add]=newline;
cout<<marry[add]<<endl;
add++;
i++;
}
file.close();
//Open the file to rewrite data from memory to it
file.open("test.txt",ios:: out|ios::in|ios::binary|ios::trunc);
for(int j=0;j<add;j++){
file<<marry[j]<<endl;
}
file.close();
return 0;
}

Thanks


Hello,
No problems with the code. The problem lies with eof() function. Try some googling and check the problem.
Nov 13 '06 #2
abdoelmasry
104 New Member
hi vpawizard
i always got Error Writing To Memory message

i solved my problem by writing the edited line directly to another file
cose of :
if i want to work with large file size
my code can't inject all lines in memory or that's
will stop operating system

thank you for trying
but i still waiting for suggestions from any memper

bye
Nov 13 '06 #3

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

Similar topics

1
5914
by: Steve Bennett | last post by:
Can anyone suggest a good tutorial for reading and writing binary files in Perl or send me some tips or examples? I tried using pack and unpack and seek and read but things don't work as expected...
4
9014
by: Igor Shulgin | last post by:
Hi! What is standard and direct way (within Oracle 9.2 stored procedures) for writing binary data from Oracle to external file on disk? We have tried to use UTL_FILE package, but it operates on...
4
4682
by: Dmitry | last post by:
Hi! Does anybody know how to write binary data to registry? The purpose is to store an boxed object into registry and to restore it.
2
1348
by: Peter Steele | last post by:
I have a managed C++ project that contains a simple structure: struct { <multiple members> } mydata; I want to write the contents of this structure out to disk retaining the exactly binary...
2
1487
by: Denise Smith | last post by:
Hello, I'm wondering if anyone can help me out here? I want to be able to browse records in a database where one of the fields contains an image. I think I might have to extract the image...
14
4115
by: Vertilka | last post by:
I need to read binary data file written by C++ program, using my C# application. How do i marshal the bytes i read with my C# code to .NET types. The data is numbers (integers float doubles...
2
14245
by: cesco | last post by:
Hi, I have a binary file containing 1000 floating point numbers. I want to load that file into an array. A way to do it could be the following: >>> import array >>> data = array.array('f')...
1
2717
by: RML | last post by:
Hi, I have a MFC C++ application which write the data in a structure out to a file. Here is the structure... typdef struct { short ID; TCHAR Num; short x; } TestStruct;
0
2860
by: Carl Forsman | last post by:
I have some binary data encoded as Base64 encoding in an XML element like this: <data>TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx</data> how can I extract this data -...
0
7121
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7162
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,...
1
6881
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...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4899
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...
0
4584
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...
0
3088
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...
1
650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.