473,324 Members | 2,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

c++ fstream writing to existing file

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
Jul 22 '05 #1
3 3631
On 23 Jul 2004 18:01:04 -0700, Mahesha <yc******@hotmail.com> wrote:
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
Jul 22 '05 #2
On 23 Jul 2004 18:01:04 -0700, yc******@hotmail.com (Mahesha) wrote in
comp.lang.c++:
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>.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #3
yc******@hotmail.com (Mahesha) wrote in message news:<3d**************************@posting.google. com>...
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
6
by: Armando | last post by:
Hallo ! I habe some error in my programm,because i use <fstream.h>,I want to use <fstream> but i don´t know which fonctions i must modify in my program ? Thanks you for your help. Armando.
14
by: Mike - EMAIL IGNORED | last post by:
Using RH9, from this fragment: fstream theStream_; string fPath = "myFile"; int mode = O_EXCL | O_CREAT; theStream_.open(fPath.c_str(),mode); I get the failure shown below. ...
9
by: Someonekicked | last post by:
In my program, I need to open multiple files, and I wont know till after the program execution how many of them (user will enter that value). So I am using a vector of fstream. I am using fstream...
2
by: horneye | last post by:
After i use fstream to open a file, how can i use the fstream object to clear file's content?
6
by: wiso | last post by:
My problem is this (from: http://www.cplusplus.com/ref/iostream/fstream/open.html) #include <fstream> using namespace std; int main() { fstream f;
5
by: neowillis | last post by:
code: #include <iostream> #include <fstream> #include <string> using namespace std; int main() {
6
by: Gaijinco | last post by:
Should this do something? #include <fstream> #include <string> int main() { std::fstream filestr ("test.txt", std::fstream::in | std::fstream::out); std::string s="";
1
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.