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 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
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
"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
"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
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.
"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
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.
> 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
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.
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.
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.
"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
"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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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;
|
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();
|
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...
|
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....
|
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>
...
|
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 =...
|
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...
|
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();...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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
|
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...
|
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...
| |