By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,400 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

Opening, reading and writing binary data to file

abdoelmasry
100+
P: 104
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
Share this Question
Share on Google+
2 Replies


vpawizard
P: 66
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
100+
P: 104
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

Post your reply

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