c++ fstream writing to existing file

M

Mahesha

Hello,
I'm new to C++ and I have requirement to open a existing text file in
write mode and write 2 new lines of text in the beginning of the file.
I'm working with fstream standard library.
If someone could direct me to a sample code to do this that would be
really helpful.

Thanks,
Mahesha
 
J

John Harrison

Hello,
I'm new to C++ and I have requirement to open a existing text file in
write mode and write 2 new lines of text in the beginning of the file.
I'm working with fstream standard library.
If someone could direct me to a sample code to do this that would be
really helpful.

Thanks,
Mahesha

That cannot be done in any file system that I know of. With files you can
write to the end of a file, or you can write over the existing contents of
a file, but you cannot insert data into the beginning or middle of a file.

I would try an redesign your program so that you don't have to do this.
You are trying to use files in a way that they were not designed to be
used.

If you really cannot avoid this, then the only way to do what you want is
to write your two lines to a new file, and then copy the rest of the old
file to the end of the new file. This is obviously very inefficient and is
why you should try and avoid if at all possible.

John
 
J

Jack Klein

Hello,
I'm new to C++ and I have requirement to open a existing text file in
write mode and write 2 new lines of text in the beginning of the file.
I'm working with fstream standard library.
If someone could direct me to a sample code to do this that would be
really helpful.

Thanks,
Mahesha

This can't be done this way in standard C++, and probably can't be
done this way even with non-standard platform specific extensions on
most platforms.

There is no function to "push down" the existing contents of a file to
make room for some arbitrary number of new characters.

What you need to do is create a file with some temporary name. Write
your two new lines to it, then copy the data from the original file
into it afterwards. After closing both files, use std::remove() to
delete the original file, and std::rename() to rename the temporary
file to the original name.

Both of the functions I mentioned are prototyped in <cstdio>.
 
J

Jerry Coffin

Hello,
I'm new to C++ and I have requirement to open a existing text file in
write mode and write 2 new lines of text in the beginning of the file.
I'm working with fstream standard library.
If someone could direct me to a sample code to do this that would be
really helpful.

Most file systems will only let you add to the end of a file. The
usual way to handle this is:
1. copy current file contents to a temporary file.
2. rewind the original file.
3. write your two lines to the file.
4. copy the contents from the temporary file back to the original.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top