473,750 Members | 2,253 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<fstrea m>
#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.t ellp();
//get line from file
getline(file,ml ine);
//copy new line from main line to edit
newline=mline;
//edit the line as string(replace VB with C++)
spos=newline.fi nd("vb");
while(spos!=str ing::npos){
newline.replace (spos,2,"c++");
spos=newline.fi nd("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::t runc);
for(int j=0;j<add;j++){
file<<marry[j]<<endl;
}
file.close();
return 0;
}

Thanks
Nov 10 '06 #1
2 4827
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<fstrea m>
#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.t ellp();
//get line from file
getline(file,ml ine);
//copy new line from main line to edit
newline=mline;
//edit the line as string(replace VB with C++)
spos=newline.fi nd("vb");
while(spos!=str ing::npos){
newline.replace (spos,2,"c++");
spos=newline.fi nd("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::t runc);
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
5947
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 so I must be missing some subtle details. For example: I tried reading a .RAW image file (no header info) and writing it back out using seek read and print. It almost worked but any part of the image that was black
4
9057
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 files consisting out of "line" of size no more than 32767 bytes and adds new_line symbol upon closing files. Also we checked out interMedia types and packages for writing BLOB to disk but now we get stuck with some strange error messages.
4
4711
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
1374
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 data without any additional header information, and of course be able to later read it back in. What's the best way to accomplish this?
2
1500
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 to a byte array, write it to a temp file then refer to the temp file as the URL source for the Image control.
14
4150
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 etc..) and strings. I looked at the Marshal class but did not know how to use it with file. Please add few code lines. Thanks alot
2
14354
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') >>> f = open('FileName.bin', 'rb') >>> data.fromfile(f, 1000)
1
2737
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
2883
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 - TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx and save to a file say test2.xml
0
9575
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...
1
9338
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,...
0
9256
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6803
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
6080
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
4885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2223
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.