473,507 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

overwrite an fstream object

I would like to write over a file if it exists (or create one if
it doesn't exist). The following codes do nothing if the file
already exists. How do I get it to overwrite an existing file?

Also, if the existing file is longer than what's going to be
written, how do I erase the excess portion of the existing file?

fsF.open ("test.txt", ios::out|ios::trunc);
if (fsF.fail())
{
cout << "File test.txt cannot be opened.";
fsF.clear();
}

fsF << something

fsF.close();
Oct 16 '05 #1
3 4833

"John Smith" <js****@company.com> wrote in message
news:GD********************@twister.southeast.rr.c om...
I would like to write over a file if it exists (or create one if
it doesn't exist). The following codes do nothing if the file
already exists. How do I get it to overwrite an existing file?
std::ofstream ofs("filename");

if(ofs)
std::cerr << "file created or truncated\n";
else
std::cerr << "failed to create or truncate file\n";

Also, if the existing file is longer than what's going to be
written, how do I erase the excess portion of the existing file?


See above.

-Mike
Oct 16 '05 #2
Mike Wahler wrote:

std::ofstream ofs("filename");

if(ofs)
std::cerr << "file created or truncated\n";
else
std::cerr << "failed to create or truncate file\n";


Thanks. Changing from fstream to ofstream did the trick, but why?
Oct 16 '05 #3

"John Smith" <js****@company.com> wrote in message
news:uZ******************@twister.southeast.rr.com ...
Mike Wahler wrote:

std::ofstream ofs("filename");

if(ofs)
std::cerr << "file created or truncated\n";
else
std::cerr << "failed to create or truncate file\n";


Thanks. Changing from fstream to ofstream did the trick, but why?


Read about the default parameters for the various fstream types.

-Mike
Oct 17 '05 #4

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

Similar topics

6
3188
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.
3
584
by: David Blasdell | last post by:
This appears very strange to me, I'm having a problem with a fstream object inside a class which is not being called directly from main (another class calls the class that contains the fstream...
1
4439
by: Someonekicked | last post by:
I have a binary file, and I need to delete a specific number of characters in it. The file contains records, each record has a specific length. So the way I wanna handle deleting is that I will...
0
1006
by: Ramsey | last post by:
I have a unmanaged lib file that creates an fstream object. This lib file is used by a managed dll. Unfortunately when I run the code it crashes when I create the fstream object. I have a...
1
5718
by: ma740988 | last post by:
Trying to some of my reading on file manipulation (ifstream/ofstream/fstream) into action. So now consider. # include <iostream> # include <fstream> # include <cstring> # include <sstream> ...
6
17099
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;
1
1724
emaghero
by: emaghero | last post by:
Greetings and Salutations, I have started using the fstream class in C++ to create text files for input and output. Whenever I try to instantiate an fstream object using the following code ...
6
3718
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="";
6
1953
by: canilao | last post by:
Hi All, So I ran into an issue with using the ! operator with fstream: // Make sure the file is ready to be read before we move on. 1: fstream fileStream; 2: ...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7114
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5045
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1544
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.