473,587 Members | 2,509 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating a file that doesn't exist

AJ
Hello, I was wondering if it is possible to create a file if it doean't
exist. I am using fstream and writing in and out a binary file. I check to
see if the file is opened. if it doesn't get opened, i want to create the
file. I was wondering if this can be done. This is the code i have for
opening the file:

fstream rfile("names.db ", ios::binary| ios::in | ios::out);
if ( !rfile ) {
cerr << "Could not open file 'names.db'\n";
}

I am hoping to put the creating of the file after the cerr. i hope someone
can help me out with this. thanks.

AJ
Jul 22 '05 #1
3 1810
Instead of rfile, don't you need to use: open??
I'm a newbie, so correct me if i'm wrong...
Jakob
"AJ" <aj******@shaw. ca> wrote in message
news:bJzDb.7222 36$pl3.454765@p d7tw3no...
Hello, I was wondering if it is possible to create a file if it doean't
exist. I am using fstream and writing in and out a binary file. I check to
see if the file is opened. if it doesn't get opened, i want to create the
file. I was wondering if this can be done. This is the code i have for
opening the file:

fstream rfile("names.db ", ios::binary| ios::in | ios::out);
if ( !rfile ) {
cerr << "Could not open file 'names.db'\n";
}

I am hoping to put the creating of the file after the cerr. i hope someone
can help me out with this. thanks.

AJ

Jul 22 '05 #2
Jakob Olsen wrote:
"AJ" <aj******@shaw. ca> wrote in message
news:bJzDb.7222 36$pl3.454765@p d7tw3no...
Hello, I was wondering if it is possible to create a file if it doean't
exist. I am using fstream and writing in and out a binary file. I check to
see if the file is opened. if it doesn't get opened, i want to create the
file. I was wondering if this can be done. This is the code i have for
opening the file:

fstream rfile("names.db ", ios::binary| ios::in | ios::out);
if ( !rfile ) {
cerr << "Could not open file 'names.db'\n";
}

I am hoping to put the creating of the file after the cerr. i hope someone
can help me out with this. thanks.

AJ

Instead of rfile, don't you need to use: open??
I'm a newbie, so correct me if i'm wrong...
Jakob

Jakob:
Don't top-post. Replies are either interspersed or
appended at the bottom, like this one.

The OP is using the one of the fstream constructors. This
is perfectly legal.
OP:
Please note that if an open fails, there may be an operating
system issue that won't permit another file being created.
Try clearing the stream errors (fstream::clear ()) and trying
to use the open() method. I'm guaranteeing that this will
work, since there is no guarantee after the constructor fails.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #3
"Jakob Olsen" <ja******@tisca li.dk> wrote in message news:<3f******* *************** @dread14.news.t ele.dk>...
Instead of rfile, don't you need to use: open??
I'm a newbie, so correct me if i'm wrong...
Jakob
He can do both :)
"AJ" <aj******@shaw. ca> wrote in message
news:bJzDb.7222 36$pl3.454765@p d7tw3no...
Hello, I was wondering if it is possible to create a file if it doean't
exist. I am using fstream and writing in and out a binary file. I check to
see if the file is opened. if it doesn't get opened, i want to create the
file. I was wondering if this can be done. This is the code i have for
opening the file:

fstream rfile("names.db ", ios::binary| ios::in | ios::out);
if ( !rfile ) {
cerr << "Could not open file 'names.db'\n";
}

I am hoping to put the creating of the file after the cerr. i hope someone
can help me out with this. thanks.

AJ


Well at least with ofstream, if you open a file that doesn't exist it
is automatically created... So I guess that is that same for its
parent class (fstream).
Jul 22 '05 #4

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

Similar topics

13
7185
by: jenny | last post by:
Hi, I am trying to find a VB way that would create a folder on all existing drives - the folder name would be the same on each drive. ie c:\backup, d:\backup, etc. But the folders would only be created if they don't already exist, and if the drive happens to be one a folder cannot be created on (ie a cdrom drive) it would just be skipped without the code generating any errors. your help on this would be most appreciated. jenny
3
7482
by: phantom | last post by:
Greetings, I have a sp that dumps text into a textfile but I am having trouble creating the textfile. EXEC master.dbo.xp_cmdShell '\\servername\d$\The File\sub\filename.dat' The directory "The File" has a space in it. I've tried putting the carat ^ before the space, and putting double quotes...but I keep getting this error
1
1446
by: Dave | last post by:
Hello, I'm using php4 and i want to check for a file, if it doesn't exist it should be created. My problem is i have to create it with specific permissions specifically 666 so that the script can then write to it. I don't have ftp access. I'm also looking for any tutorials, recent ones, using php4 for creating file upload areas, i'm trying to make one of those as well. Thanks. Dave. Thanks.
2
10379
by: David Elliott | last post by:
I am creating a configuration class to read / write a standard configuration file. <?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="ConnectionString" value="server=(local);" /> </appSettings> </configuration> I am using a XmlDocument for my base implementation.
0
1939
by: Andrew | last post by:
Hi, I have just installed the server components of Visual Studio 2003 Enterprise Architect Edition on a server with Windows 2000 Advanced Server (SP3). When I attempt to create a new Web project on this server I get the following message.... The web server reported the following error attempting to create or open the web project locate at the following
5
1153
by: VB Programmer | last post by:
I want to write to a simple text file. If it doesn't exist I want to create it first. Here is my code (portion): Imports System.IO ' at the top of the class : : Dim strFileName As String = "C:\MyFile.txt" ' create file if not exist If Not File.Exists(strfilename) Then File.CreateText(strFileName)
7
3917
by: Selden McCabe | last post by:
I'm using the following code to write some text to a file: objWriter = New StreamWriter(FullPath, True, System.Text.Encoding.ASCII) For nRow = 1 To sData.Length - 1 objWriter.WriteLine(sData(nRow)) Next nRow objWriter.Flush() objWriter.Close() When I run this in the IDE, it works. When I compile it and run the EXE, it
3
2086
by: Adam Short | last post by:
I have a webservice which takes a hostname and drive letter as input and returns the free space for that drive on that host as a long. Sometimes it will take forever to get information from a recalcitrant host, but it keeps trying far longer than I want it to. How can I set up a timeout for the process, so that after a couple of seconds it returns a failure status and gives up waiting? Currently, the method maps the drive, finds its...
3
4493
by: Sean C. | last post by:
Hey All, I'm having a little problem here. I have a project that I'm working on that involves a MySQL server database backend. I'm having no problem creating the database on the fly if it doesn't already exist and using it once it's created. My problem comes into play when I run the program for the first time. I'm wanting to have some kind of flag that lets me know that the database has not yet been created, so that I can call my...
0
7923
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
7852
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,...
0
8216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7974
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
8221
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5395
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();...
0
3845
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
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

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.