Thread: RE: [Dev-C++] File Existence
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <Mat...@Ve...> - 2005-05-19 18:02:48
|
Also when you open a stream, checking it like a boolean will fail if the
file open failed:
For example:
#include <ifstream>
#include <stdexcept>
...
ifstream foo;
if(!foo) throw runtime_error("Could not open file.");
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...]On Behalf Of Farzan
Hajian
Sent: Friday, May 20, 2005 2:31 AM
To: dev...@li...
Subject: [Dev-C++] File Existence
Hi,
I'v written a small program which simulates the DOS command "type". The
program receives
a file name form the command line. I used "fail()" function to check
existence of the received
file. I have not used "ifstream" functions a lot and I want to know
whether this is the
proper way to do so or not.
There is another function called "bad()". Please tell me what kind of
errors it can detect.
I tried it in my program and it couldn't check existence of files.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main(int argc,char* arg[])
{
if(argc==2)
{
ifstream File(arg[1]);
char ch;
if(File.fail())
cout << "File not found!\n";
else
while(true)
{
File.get(ch);
if(File.eof())
break;
cout << ch;
}
}
system("pause>null");
}
Thanks.
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
-------------------------------------------------------
This SF.Net email is sponsored by Oracle Space Sweepstakes
Want to be the first software developer in space?
Enter now for the Oracle Space Sweepstakes!
http://ads.osdn.com/?ad_idt12&alloc_id344&op=click
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
___________________________________________________________________
The information contained in this message and any attachment may be
proprietary, confidential, and privileged or subject to the work
product doctrine and thus protected from disclosure. If the reader
of this message is not the intended recipient, or an employee or
agent responsible for delivering this message to the intended
recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited.
If you have received this communication in error, please notify me
immediately by replying to this message and deleting it and all
copies and backups thereof. Thank you.
|
|
From: <A_B...@gm...> - 2005-05-19 23:20:32
|
Well, it's a little bit unconventional, but I check file existans in this way:
if(FILE* f = fopen("file", "r")){
fclose(f);
// file exists
...
}else{
// file doesn't exist
...
}
What speaks against this method ???
CU, Acki
|
|
From: Per W. <pw...@ia...> - 2005-05-20 07:03:20
|
Have you looked at the access() function - specifically there to check for
existence - or specific rights - for a file.
/Per W
On Fri, 20 May 2005 A_B...@gm... wrote:
> Well, it's a little bit unconventional, but I check file existans in this way:
>
> if(FILE* f = fopen("file", "r")){
> fclose(f);
> // file exists
> ...
> }else{
> // file doesn't exist
> ...
> }
>
> What speaks against this method ???
>
> CU, Acki
|
|
From: Gilles C. <cre...@ya...> - 2005-05-20 07:43:30
|
Hello,
with function "fstat" , you have possibility to
receive size of file , owner ,.... and existence (+
portability).
Gilles
--- Per Westermark <pw...@ia...> a écrit:
> Have you looked at the access() function -
> specifically there to check for
> existence - or specific rights - for a file.
>
> /Per W
>
> On Fri, 20 May 2005 A_B...@gm... wrote:
>
> > Well, it's a little bit unconventional, but I
> check file existans in this way:
> >
> > if(FILE* f = fopen("file", "r")){
> > fclose(f);
> > // file exists
> > ...
> > }else{
> > // file doesn't exist
> > ...
> > }
> >
> > What speaks against this method ???
> >
> > CU, Acki
>
>
>
>
-------------------------------------------------------
> This SF.Net email is sponsored by Oracle Space
> Sweepstakes
> Want to be the first software developer in space?
> Enter now for the Oracle Space Sweepstakes!
>
http://ads.osdn.com/?ad_id=7412&alloc_id=16344&op=click
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE:
> http://www23.brinkster.com/noicys/devcpp/ub.htm
>
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
_____________________________________________________________________________
Découvrez le nouveau Yahoo! Mail : 1 Go d'espace de stockage pour vos mails, photos et vidéos !
Créez votre Yahoo! Mail sur http://fr.mail.yahoo.com
|
|
From: Per W. <pw...@ia...> - 2005-05-20 09:18:43
|
You mean the portability that have made M$ rename that function to
_fstat() - at the same time they renamed _mkdir(), _open(), _close(),
_read(), _write(), _chmod(), _access(), _chdir(), ...
For just quick-checking existence or read or write access, access() is the
nicest function - no structs and extra tests needed.
stat(), fstat(), lstat() are more powerful, but requires more work - extra
include files and a struct stat variable.
On the other hand. If the intention is to actually open and/or create the
file, there is normally no need to pre-check existence. The used open
command can handle it.
/Per W
On Fri, 20 May 2005, Gilles Crebassa wrote:
> Hello,
>
> with function "fstat" , you have possibility to
> receive size of file , owner ,.... and existence (+
> portability).
>
>
> Gilles
>
> --- Per Westermark <pw...@ia...> a =E9crit:
> > Have you looked at the access() function -
> > specifically there to check for
> > existence - or specific rights - for a file.
> >
> > /Per W
> >
> > On Fri, 20 May 2005 A_B...@gm... wrote:
> >
> > > Well, it's a little bit unconventional, but I
> > check file existans in this way:
> > >
> > > if(FILE* f =3D fopen("file", "r")){
> > > fclose(f);
> > > // file exists
> > > ...
> > > }else{
> > > // file doesn't exist
> > > ...
> > > }
> > >
> > > What speaks against this method ???
> > >
> > > CU, Acki
|