944,167 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 6856
  • C RSS
Jun 19th, 2006
0

ostream - how to insert a line of text

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jeffxiang is offline Offline
2 posts
since Jun 2006
Jun 19th, 2006
0

Re: ostream - how to insert a line of text

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Jun 19th, 2006
0

Re: ostream - how to insert a line of text

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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jun 19th, 2006
0

Re: ostream - how to insert a line of text

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)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xgills is offline Offline
3 posts
since Apr 2006
Jun 20th, 2006
0

Re: ostream - how to insert a line of text

Thanks for help, it is good idear. I will try that.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jeffxiang is offline Offline
2 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Increase form size beyond screen resolution
Next Thread in C Forum Timeline: waitpid w/ multiple processes





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC