473,387 Members | 1,641 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,387 software developers and data experts.

fstream..

hi there
i have been trying to open file using fstream, in read+write+binary mode

fstream ff;

ff.open(filename, ios::in | ios::out | ios::binary);

now this works

what i m looking for is, if the file does not exist, it should be created,
and i dont know what ios flag to use, the above statement will open the file
only if it already exist, for now what i m doing is chaek wether the file
already exist, if it does not exist then it opens file just for writing and
creates a zero byte file and then closes, then i m able to use above
statement for writing and reading from it, m i doing it right, any
suggestion ??

regards
Jul 22 '05 #1
3 1712

"dumboo" <vt***@yahoo.com> wrote in message
news:2g************@uni-berlin.de...
hi there
i have been trying to open file using fstream, in read+write+binary mode

fstream ff;

ff.open(filename, ios::in | ios::out | ios::binary);

now this works

what i m looking for is, if the file does not exist, it should be created,
and i dont know what ios flag to use, the above statement will open the file only if it already exist, for now what i m doing is chaek wether the file
already exist, if it does not exist then it opens file just for writing and creates a zero byte file and then closes, then i m able to use above
statement for writing and reading from it, m i doing it right, any
suggestion ??

regards


There's no one set of flags that will do what you want. Try this

ff.open(filename, ios::in | ios::out | ios::binary);
if (!ff.is_open())
{
ff.open(filename, ios::in | ios::out | ios::trunc | ios::binary);
if (!ff.is_open())
error();
}

The second open call will create a zero length file, open for reading and
writing.

john
Jul 22 '05 #2
hi there
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2g************@uni-berlin.de...
[..]
There's no one set of flags that will do what you want. Try this

ff.open(filename, ios::in | ios::out | ios::binary);
if (!ff.is_open())
{
ff.open(filename, ios::in | ios::out | ios::trunc | ios::binary);
if (!ff.is_open())
error();
}

[..]
thats wt i m doing now, it was strange to read that there is no set of flags
for doing such a common operation, any way thankx for your suggestion :-)
regards
Jul 22 '05 #3
dumboo <vt***@yahoo.com> wrote:
thats wt i m doing now, it was strange to read that there is no set of flags
for doing such a common operation, any way thankx for your suggestion :-)
regards


If you compare the available flags with standard C's fopen() family of calls,
you'll notice a striking similarity. :-) fopen() doesn't have this feature
either.

I was surprised to find this as well, but I was so used to POSIX open() that
it caught me off guard.

- Chris

Jul 22 '05 #4

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

Similar topics

6
by: David Briggs | last post by:
I am using MS VC++ 6.0 with MFC I have a simple class: #include <fstream.h> class Data { public: CString WriteStr(); Data();
3
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...
3
by: ratzeel | last post by:
The following snippet code throws an error while compiling on SUN os.. Any idea how to resolve this... #include <iostream.h> #include <fstream.h> #include <math.h> #include <algorithm>...
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...
1
by: MForey | last post by:
I'm attempting to create a program that uses fstream objects to read/write to files. However, it is currently balky at best. The fstream.write call doesn't return an error, but the modified data...
5
by: ehui928 | last post by:
The following program is used to open a file in mode both read and append, but it has a problem that the file can't be opened. #include <iostream> #include <fstream> #include <cstdlib> ...
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() {
7
by: Soneji | last post by:
*sigh* Ok, I have two questions. First, my setup: Win-doze XP-SP2; & Dev-C++ 4.9.9.2; I recently started trying to use 'fstream' to work my input/output, and I noticed that using the...
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="";
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...

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.