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

problem opening fstream

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. According to the man pages and
the standard, the second argument should be an int.
Thanks in advance for your help.

Mike.

--

g++ -I. -I../MdbUtil -c -o UniqueFile.o UniqueFile.cc
UniqueFile.cc: In member function `void MdbUtil::UniqueFile::open(const char*,
const char*, int, int, const char*)':
UniqueFile.cc:64: invalid conversion from `int' to `std::_Ios_Openmode'
UniqueFile.cc:64: initializing argument 2 of `void std::basic_fstream<_CharT,
_Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits
= std::char_traits<char>]'
make: *** [UniqueFile.o] Error 1
Jul 22 '05 #1
14 3704
"Mike - EMAIL IGNORED" <m_*************@yahoo.com> wrote...
Using RH9, from this fragment:

fstream theStream_;
string fPath = "myFile";
int mode = O_EXCL | O_CREAT;
theStream_.open(fPath.c_str(),mode);
What does 'O_EXCL | O_CREAT' expression doing in 'fstream::open'?
You are supposed to use constants from 'std::ios_base'...

I get the failure shown below. According to the man pages and
the standard, the second argument should be an int.
According to what standard? What page/clause/paragraph? My copy of the
standard states that the second argument has the type
'std::ios_base::openmode'.
Thanks in advance for your help.

Mike.

--

g++ -I. -I../MdbUtil -c -o UniqueFile.o UniqueFile.cc
UniqueFile.cc: In member function `void MdbUtil::UniqueFile::open(const char*, const char*, int, int, const char*)':
UniqueFile.cc:64: invalid conversion from `int' to `std::_Ios_Openmode'
UniqueFile.cc:64: initializing argument 2 of `void std::basic_fstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]'
make: *** [UniqueFile.o] Error 1

Jul 22 '05 #2
Yes, I see my error now. My requirement is to open an ofstream,
and have it fail if the file already exists. I looked at the
constants in std::ios_base, and like the old fstream capability,
it does not appear to have the semantics to do this. If I am
correct about this, what is the best way to check for the
existence of a file?
Thanks again for your help,
Mike.
Victor Bazarov wrote:

"Mike - EMAIL IGNORED" <m_*************@yahoo.com> wrote...
Using RH9, from this fragment:

fstream theStream_;
string fPath = "myFile";
int mode = O_EXCL | O_CREAT;
theStream_.open(fPath.c_str(),mode);


What does 'O_EXCL | O_CREAT' expression doing in 'fstream::open'?
You are supposed to use constants from 'std::ios_base'...

I get the failure shown below. According to the man pages and
the standard, the second argument should be an int.


According to what standard? What page/clause/paragraph? My copy of the
standard states that the second argument has the type
'std::ios_base::openmode'.
Thanks in advance for your help.

Mike.

--

g++ -I. -I../MdbUtil -c -o UniqueFile.o UniqueFile.cc
UniqueFile.cc: In member function `void MdbUtil::UniqueFile::open(const

char*,
const char*, int, int, const char*)':
UniqueFile.cc:64: invalid conversion from `int' to `std::_Ios_Openmode'
UniqueFile.cc:64: initializing argument 2 of `void

std::basic_fstream<_CharT,
_Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char,

_Traits
= std::char_traits<char>]'
make: *** [UniqueFile.o] Error 1

Jul 22 '05 #3
"Mike - EMAIL IGNORED" <m_*************@yahoo.com> wrote...
Victor Bazarov wrote:

"Mike - EMAIL IGNORED" <m_*************@yahoo.com> wrote...
Using RH9, from this fragment:

fstream theStream_;
string fPath = "myFile";
int mode = O_EXCL | O_CREAT;
theStream_.open(fPath.c_str(),mode);


What does 'O_EXCL | O_CREAT' expression doing in 'fstream::open'?
You are supposed to use constants from 'std::ios_base'...

I get the failure shown below. According to the man pages and
the standard, the second argument should be an int.


According to what standard? What page/clause/paragraph? My copy of the
standard states that the second argument has the type
'std::ios_base::openmode'.
Thanks in advance for your help.

Mike.

--

g++ -I. -I../MdbUtil -c -o UniqueFile.o UniqueFile.cc
UniqueFile.cc: In member function `void MdbUtil::UniqueFile::open(const
char*,
const char*, int, int, const char*)':
UniqueFile.cc:64: invalid conversion from `int' to
`std::_Ios_Openmode' UniqueFile.cc:64: initializing argument 2 of `void

std::basic_fstream<_CharT,
_Traits>::open(const char*, std::_Ios_Openmode) [with _CharT =

char, _Traits
= std::char_traits<char>]'
make: *** [UniqueFile.o] Error 1


Yes, I see my error now. My requirement is to open an ofstream,
and have it fail if the file already exists. I looked at the
constants in std::ios_base, and like the old fstream capability,
it does not appear to have the semantics to do this. If I am
correct about this, what is the best way to check for the
existence of a file?


(a) Please don't top-post

(b) The only way I know in C++ to check if a file exists is to open
the file for reading [only] and check whether it succeeds. My
guess is that if it doesn't succeed (there is no such file), then
you re-open the stream for writing and continue using it (if I
understood your intentions correctly).

Victor
Jul 22 '05 #4

"Mike - EMAIL IGNORED" <m_*************@yahoo.com> wrote in message
news:40***************@yahoo.com...
Yes, I see my error now. My requirement is to open an ofstream,
and have it fail if the file already exists. I looked at the
constants in std::ios_base, and like the old fstream capability,
it does not appear to have the semantics to do this. If I am
correct about this, what is the best way to check for the
existence of a file?
Thanks again for your help,
Mike.


You can use ios_base::in|ios_base::out, its equivalent to the stdio "r+"
mode, i.e. open an existing file for reading and writing.

john
Jul 22 '05 #5


John Harrison wrote:

"Mike - EMAIL IGNORED" <m_*************@yahoo.com> wrote in message
news:40***************@yahoo.com...
Yes, I see my error now. My requirement is to open an ofstream,
and have it fail if the file already exists. I looked at the
constants in std::ios_base, and like the old fstream capability,
it does not appear to have the semantics to do this. If I am
correct about this, what is the best way to check for the
existence of a file?
Thanks again for your help,
Mike.


You can use ios_base::in|ios_base::out, its equivalent to the stdio "r+"
mode, i.e. open an existing file for reading and writing.

john


Since I am on Linux, and the code is system dependent in other
ways, I am thinking of using system("test -e myFile"), which returns
256 if the file does not exist.

Mike.
Jul 22 '05 #6

"Mike - EMAIL IGNORED" <m_*************@yahoo.com> wrote in message
news:40***************@yahoo.com...


John Harrison wrote:

"Mike - EMAIL IGNORED" <m_*************@yahoo.com> wrote in message
news:40***************@yahoo.com...
Yes, I see my error now. My requirement is to open an ofstream,
and have it fail if the file already exists. I looked at the
constants in std::ios_base, and like the old fstream capability,
it does not appear to have the semantics to do this. If I am
correct about this, what is the best way to check for the
existence of a file?
Thanks again for your help,
Mike.


You can use ios_base::in|ios_base::out, its equivalent to the stdio "r+"
mode, i.e. open an existing file for reading and writing.

john


Since I am on Linux, and the code is system dependent in other
ways, I am thinking of using system("test -e myFile"), which returns
256 if the file does not exist.

Mike.


But why test for file existence? My option avoids the need to test for file
existence.

fstream f("myfile", ios_base::in|ios_base::out); // if the file does not
exist the open will fail

or am i missing something?

john
Jul 22 '05 #7


John Harrison wrote:

"Mike - EMAIL IGNORED" <m_*************@yahoo.com> wrote in message
news:40***************@yahoo.com...


John Harrison wrote:

"Mike - EMAIL IGNORED" <m_*************@yahoo.com> wrote in message
news:40***************@yahoo.com...
> Yes, I see my error now. My requirement is to open an ofstream,
> and have it fail if the file already exists. I looked at the
> constants in std::ios_base, and like the old fstream capability,
> it does not appear to have the semantics to do this. If I am
> correct about this, what is the best way to check for the
> existence of a file?
> Thanks again for your help,
> Mike.
>

You can use ios_base::in|ios_base::out, its equivalent to the stdio "r+"
mode, i.e. open an existing file for reading and writing.

john


Since I am on Linux, and the code is system dependent in other
ways, I am thinking of using system("test -e myFile"), which returns
256 if the file does not exist.

Mike.


But why test for file existence? My option avoids the need to test for file
existence.

fstream f("myfile", ios_base::in|ios_base::out); // if the file does not
exist the open will fail

or am i missing something?

john


Yes, you are missing my basic (and perhaps unusual) requirement stated
above and repeated here:

My requirement is to open an ofstream, and have it fail
if the file already exists.

It is the converse of the requirement that your suggestion satisfies.
My requirement is satisfied by:

int open("myFile", O_CREAT | O_EXCL)

as specified in IEEE Std 1003.1-2001, but I need it for std::fstream.

Additional suggestions are very welcome.

Thanks,
Mike.
Jul 22 '05 #8
>
Yes, you are missing my basic (and perhaps unusual) requirement stated
above and repeated here:

My requirement is to open an ofstream, and have it fail
if the file already exists.

It is the converse of the requirement that your suggestion satisfies.
My requirement is satisfied by:

int open("myFile", O_CREAT | O_EXCL)

as specified in IEEE Std 1003.1-2001, but I need it for std::fstream.

Additional suggestions are very welcome.

Thanks,
Mike.


Yes my apologies, I read it several times even now before I got it.

Rather than use non-standard code (even if other parts of you app do) I
would take Victor's suggestion and first try to open the file for reading
only.

John
Jul 22 '05 #9
Mike - EMAIL IGNORED wrote:

Since I am on Linux, and the code is system dependent in other
ways, I am thinking of using system("test -e myFile"), which returns
256 if the file does not exist.


Bad idea, do a man page on stat.
Jul 22 '05 #10
Jorge Rivera wrote:

Mike - EMAIL IGNORED wrote:

Since I am on Linux, and the code is system dependent in other
ways, I am thinking of using system("test -e myFile"), which returns
256 if the file does not exist.


Bad idea, do a man page on stat.


Thanks for this suggestion, and I looked at man and info. Called
from C++, I do not see how I could use it. I tried it on an existing
file and a file name that referred to no existing file. In both
cases, echo $? returned zero, indicating that the return status
does not distinguish whether a file actually exists.

Additional argument would be good. In particular,

1. Why is this better than the method that I suggest?

2. How would it be used from a C++ program?

Thanks for your help.

Mike.
Jul 22 '05 #11
John Harrison wrote:

Yes, you are missing my basic (and perhaps unusual) requirement stated
above and repeated here:

My requirement is to open an ofstream, and have it fail
if the file already exists.

It is the converse of the requirement that your suggestion satisfies.
My requirement is satisfied by:

int open("myFile", O_CREAT | O_EXCL)

as specified in IEEE Std 1003.1-2001, but I need it for std::fstream.

Additional suggestions are very welcome.

Thanks,
Mike.


Yes my apologies, I read it several times even now before I got it.

Rather than use non-standard code (even if other parts of you app do) I
would take Victor's suggestion and first try to open the file for reading
only.

John


I agree that it is generally best to avoid non-standard methods. However
I am not convinced that the "open for reading" method results in the
required certainty. What if the file exists but has its read
permissions removed? Are we sure that there is no other possible
reason for failure to open for reading?

Thanks,
Mike.
Jul 22 '05 #12
"Mike - EMAIL IGNORED" <m_*************@yahoo.com> wrote...
Jorge Rivera wrote:

Mike - EMAIL IGNORED wrote:

Since I am on Linux, and the code is system dependent in other
ways, I am thinking of using system("test -e myFile"), which returns
256 if the file does not exist.
Bad idea, do a man page on stat.


Thanks for this suggestion, and I looked at man and info. Called
from C++, I do not see how I could use it. I tried it on an existing
file and a file name that referred to no existing file. In both
cases, echo $? returned zero, indicating that the return status
does not distinguish whether a file actually exists.

Additional argument would be good. In particular,

1. Why is this better than the method that I suggest?


It could be that you need to think C++ program instead of shell.
2. How would it be used from a C++ program?


There is, IIRC, 'stat' _function_ on UNIX and clones. Since it's not
a standard function (contrary to what some man pages claim), it's OT
here, but you've got command of man pages, look it up (again), in
section 2 (functions)

V
Jul 22 '05 #13
"Mike - EMAIL IGNORED" <m_*************@yahoo.com> wrote...
John Harrison wrote:

Yes, you are missing my basic (and perhaps unusual) requirement stated
above and repeated here:

My requirement is to open an ofstream, and have it fail
if the file already exists.

It is the converse of the requirement that your suggestion satisfies.
My requirement is satisfied by:

int open("myFile", O_CREAT | O_EXCL)

as specified in IEEE Std 1003.1-2001, but I need it for std::fstream.

Additional suggestions are very welcome.

Thanks,
Mike.
Yes my apologies, I read it several times even now before I got it.

Rather than use non-standard code (even if other parts of you app do) I
would take Victor's suggestion and first try to open the file for reading only.

John


I agree that it is generally best to avoid non-standard methods. However
I am not convinced that the "open for reading" method results in the
required certainty. What if the file exists but has its read
permissions removed?


Then try opening it for writing...

File handling is very rudimental in C++ standard library. IOW, the library
was not really designed to handle all possible file situations (and some
have expressed their opinion that the library would be better off without
_any_ support for files instead of what it has now), but let us not start
on the long journey of beating that dead horse again, OK?
Are we sure that there is no other possible
reason for failure to open for reading?


There probably is. Finding holes in the language that is designed to exist
on a great variety of systems is not difficult when you narrow down your
view to only one system or a couple. If you need to solve a problem at
hand,
find a solution even though it would be platform-specific (hint: use 'stat'
function, it returns -1 if failed with errno set to ENOENT).

V
Jul 22 '05 #14
Victor Bazarov wrote:

"Mike - EMAIL IGNORED" <m_*************@yahoo.com> wrote...
Jorge Rivera wrote:

Mike - EMAIL IGNORED wrote:
>
> Since I am on Linux, and the code is system dependent in other
> ways, I am thinking of using system("test -e myFile"), which returns
> 256 if the file does not exist.

Bad idea, do a man page on stat.


Thanks for this suggestion, and I looked at man and info. Called
from C++, I do not see how I could use it. I tried it on an existing
file and a file name that referred to no existing file. In both
cases, echo $? returned zero, indicating that the return status
does not distinguish whether a file actually exists.

Additional argument would be good. In particular,

1. Why is this better than the method that I suggest?


It could be that you need to think C++ program instead of shell.
2. How would it be used from a C++ program?


There is, IIRC, 'stat' _function_ on UNIX and clones. Since it's not
a standard function (contrary to what some man pages claim), it's OT
here, but you've got command of man pages, look it up (again), in
section 2 (functions)

V


The stat function looks like the way to go. Thanks to
everyone for discussion and advice.

Mike.
Jul 22 '05 #15

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

Similar topics

3
by: Frédéric Manzanares | last post by:
hello, my problem: I want to habe one Class with write and read in a file. i have overloaded the operator >> and <<. class c_File { public : fstream fs;
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...
1
by: Alfons | last post by:
Hello, I have build a program that can do file transferring between a Windows XP computer and a DOS computer via a serial port. The Windows program I have build in C++ with Visual Studio 6.0....
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> ...
1
by: Bob Alston | last post by:
Really wierd problem opening a form with selection. I use this code on a button: On Error GoTo Err_Command8_Click Dim stDocName As String Dim stLinkCriteria As String stDocName =...
1
by: vinothg | last post by:
I have a binary file,which contains strings of 30 bytes each.I need to open the file,read the strings one by one and if the string is not found i need to write it.But unfortunately both read and...
1
by: aviraldg | last post by:
I'm having a problem with read , my code is somewhat like: char chipMemory ; // ~35XX fstream ifile; ifile.open(argv,ios::in|ios::binary|ios::nocreate|ios::ate); int fileLength=ifile.tellg();...
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
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...
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: 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...
0
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: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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.