ostream - how to insert a line of text

Reply

Join Date: Jun 2006
Posts: 2
Reputation: jeffxiang is an unknown quantity at this point 
Solved Threads: 0
jeffxiang jeffxiang is offline Offline
Newbie Poster

ostream - how to insert a line of text

 
0
  #1
Jun 19th, 2006
Hello,

ostream - how to insert a line of text instead of overwrite in the beginning of the file?

#include <fstream.h>
int main()
{
char fileName[] = "SampleFile";
char buffer[255];
ofstream m_fp;
// m_fp.open(fileName, ofstream::ate );
m_fp.open(fileName, ios::in|ios::out);
m_fp.seekp(0, ios::beg );
m_fp << "This is the line written to the file 01"<<endl;
m_fp.close();
};

SampleFile has:
11111111111111111
2222222222222222
3333333333333333
44444444444444444
5555555555555555555555

When this progrom get executed, I got:
This is the line written to the file 01
33333333333
44444444444444444
5555555555555555555555

How can I get output like this?

This is the line written to the file 01
11111111111111111
2222222222222222
33333333333
44444444444444444
5555555555555555555555


Thanks,
Jeff
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,381
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: ostream - how to insert a line of text

 
0
  #2
Jun 19th, 2006
I'm certain there is a FAQ about this. The only way to do that is to rewrite the entire file. Read the whole file into memory then write it back out the way you want it. If the file is too big to read into memory all at once you can use a temporary file.
Last edited by Ancient Dragon; Jun 19th, 2006 at 8:29 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: ostream - how to insert a line of text

 
0
  #3
Jun 19th, 2006
It's my understanding that ::ate places you at the end of the file and allows you to write data anywhere in the file, however, writing to a specific location overwrites what was previously there.. I believe you use seek() or something with that method, but I doubt that's what you're looking for. If you want to append to a file (not at the end) and preverse what was previously there then it's impossible. Go with what AG said.
Last edited by server_crash; Jun 19th, 2006 at 8:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 3
Reputation: xgills is an unknown quantity at this point 
Solved Threads: 0
xgills xgills is offline Offline
Newbie Poster

Re: ostream - how to insert a line of text

 
0
  #4
Jun 19th, 2006
Yah...you have to read it all in first...you could use this read funciton which reads it all in at once instaed of using mutlistrings or something...but usually i just add things in one line at a time...take 2 strings...

1. Make string one your original msg to insert
2. Read in the first line to string2
3. Insert string 1 (string with orignal msg)
4. Move string 2 to string 1
5. Repeat starting at step 2 until entire file copied (good time for a loop)
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 2
Reputation: jeffxiang is an unknown quantity at this point 
Solved Threads: 0
jeffxiang jeffxiang is offline Offline
Newbie Poster

Re: ostream - how to insert a line of text

 
0
  #5
Jun 20th, 2006
Thanks for help, it is good idear. I will try that.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC