473,662 Members | 2,752 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::t runc);
if (fsF.fail())
{
cout << "File test.txt cannot be opened.";
fsF.clear();
}

fsF << something

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

"John Smith" <js****@company .com> wrote in message
news:GD******** ************@tw ister.southeast .rr.com...
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******** **********@twis ter.southeast.r r.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
3196
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 object). Use the following code (stripped down from what I'm actually working on, but does has the problem I'm experiencing) Create a file called test.txt in the working directory with a few bytes of any text or data Can you explain to me why...
1
4447
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 overwrite the record I want to delete with the last record in the file, then actually delete that last record in the file. the problem Im having is how to delete that last record in the file. Suppose each record is of length 43, then I have...
0
1016
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 hunch that it is a simple problem of configuring my project file. But the only file I have linked is msvcrtd.lib.
1
5763
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> bool remove_and_create_if_exists( const std::string& filename
6
17132
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
1727
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 fstream data("c:\\sine.txt",ios_base::in|ios_base::out); if(!data){ cerr<<"Failed to open data\n"; } data.close();
6
3728
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
1957
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: fileStream.open("c:\\some_file.txt"); // Try to open until success. 3: while(!fileStream) fileStream.open("c:\\some_file.txt");
0
8435
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8547
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7368
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5655
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2763
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 we have to send another system
2
1999
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1754
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.