checking if a file/stream exists

  • Thread starter Leslaw Bieniasz
  • Start date
L

Leslaw Bieniasz

Cracow, 3.01.2005

Hello,

When opening a file stream in the append mode,
either a new file is created (if a specified file does not exist),
or an existing file is opened for adding stuff.
Is there any way to determine, after calling the open(filename,ios::app)
method, which of these two alternatives has occurred?
I would like to differentiate the program operation, depending
on whether I write to a new or existing file.

L.Bieniasz


*-------------------------------------------------------------------*
| Dr. Leslaw Bieniasz, |
| Institute of Physical Chemistry of the Polish Academy of Sciences,|
| Department of Electrochemical Oxidation of Gaseous Fuels, |
| ul. Zagrody 13, 30-318 Cracow, Poland. |
| tel./fax: +48 (12) 266-03-41 |
| E-mail: (e-mail address removed) |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
 
K

Karl Heinz Buchegger

Leslaw said:
Cracow, 3.01.2005

Hello,

When opening a file stream in the append mode,
either a new file is created (if a specified file does not exist),
or an existing file is opened for adding stuff.
Is there any way to determine, after calling the open(filename,ios::app)
method, which of these two alternatives has occurred?
I would like to differentiate the program operation, depending
on whether I write to a new or existing file.

Your system may contain a system specific function which allows yo
to check for file existence.

Another way:
Try to open the file in non-append mode. If this works, then the
file does exist, if not ... Close the file and open it in append
mode. You then can now use the information you found out in
the first open attempt to guide your operations.
 
S

Shan

You can use the ifstream::tellg() (part of STL) to find the size of the
file. If it is greater than zero then the file must have existed
before you opened it. If the file size is zero then you can assume that
the file is new..Hope it helps.

-Shan
 
K

Karl Heinz Buchegger

Shan said:
You can use the ifstream::tellg() (part of STL) to find the size of the
file. If it is greater than zero then the file must have existed
before you opened it. If the file size is zero then you can assume that
the file is new..Hope it helps.

Not really.
tellg() returns some number which can only be used for a seekg().
No one gives a guarantee of what this number represents. It can
be the number of bytes read so far, it need not be that way.
 
S

Shan

Not really.
tellg() returns some number which can only be used for a seekg().
Correct. I missed that. I wanted to give a portable solution, which i
think is not possible. I think stat() function can be used , even
though it is a C function.
 
M

Mike Wahler

Karl Heinz Buchegger said:
Not really.
tellg() returns some number which can only be used for a seekg().
No one gives a guarantee of what this number represents. It can
be the number of bytes read so far, it need not be that way.

Also a file with size zero might already exist.

-Mike
 
M

Mike Wahler

Shan said:
Correct. I missed that. I wanted to give a portable solution, which i
think is not possible. I think stat() function can be used , even
though it is a C function.

'stat()' is an extension provided by some implementations,
where it is applicable. It's not a standard C or C++ function.

-Mike
 
S

Stephen Howe

When opening a file stream in the append mode,
either a new file is created (if a specified file does not exist),
or an existing file is opened for adding stuff.
Is there any way to determine, after calling the open(filename,ios::app)
method, which of these two alternatives has occurred?
I would like to differentiate the program operation, depending
on whether I write to a new or existing file.

There is a way with standard C++ but it is cumbersome considering what you
want to do.
Open the file for input. If the open succeeds, it must exist.

Stephen Howe
 
L

Leslaw Bieniasz

Hello,

Your system may contain a system specific function which allows yo
to check for file existence.

Another way:
Try to open the file in non-append mode. If this works, then the
file does exist, if not ... Close the file and open it in append
mode. You then can now use the information you found out in
the first open attempt to guide your operations.

That's what I have been doing, but I feel this is not elegant.
Aren't there a better way?

L.B.

*-------------------------------------------------------------------*
| Dr. Leslaw Bieniasz, |
| Institute of Physical Chemistry of the Polish Academy of Sciences,|
| Department of Electrochemical Oxidation of Gaseous Fuels, |
| ul. Zagrody 13, 30-318 Cracow, Poland. |
| tel./fax: +48 (12) 266-03-41 |
| E-mail: (e-mail address removed) |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
 
K

Karl Heinz Buchegger

Leslaw said:
That's what I have been doing, but I feel this is not elegant.
Aren't there a better way?

Not that I know of.
Elegance is in the eye of the beholder.
Come on, we are talking about 3 or 4 lines of code. You can
wrap them up in a function of your own.
 
M

msalters

Karl said:
Another way:
Try to open the file in non-append mode. If this works, then the
file does exist, if not ... Close the file and open it in append
mode. You then can now use the information you found out in
the first open attempt to guide your operations.

Which is a good approach, but not perfect. The file system is
shared and not locked, so another (instance of the same) application
can create the file, after you concluded it does not exist.
Regards,
Michiel Salters
 
M

Mike Wahler

Stephen Howe said:
There is a way with standard C++ but it is cumbersome considering what you
want to do.
Open the file for input. If the open succeeds, it must exist.

But if it fails, that does not necessarily mean it does not
exist (e.g. could be locked by another user, insufficient
permissions, broken net connection, etc.). There is no
way in standard C++ to determine conclusively the existence
or nonexistence of a file.

-Mike
 
T

ToeNi

Bro,
This's what I got http://www.cplusplus.com/ref/iostream/ifstream/

at that site may solve the problem(existence of a file).

This is my logic:1.open the file anymode(ios::app, ios::in, ios::eek:ut)
2.checking is_open
example:
bool exist;
exist=(fileobject.is_open())?true:false;
3.if file is not open, which mean file is not exist, then open the file
with append mode (this is file creation).
4.After that close the file, reopen it (any mode) (depend on you).
5.if file is open, continue the others
 
M

Mike Wahler

ToeNi said:
Bro,
This's what I got http://www.cplusplus.com/ref/iostream/ifstream/

at that site may solve the problem(existence of a file).

This is my logic:
1.open the file anymode(ios::app, ios::in, ios::eek:ut)
2.checking is_open
example:
bool exist;
exist=(fileobject.is_open())?true:false;
3.if file is not open, which mean file is not exist,

It might or might not mean that. Opening a file can
fail for a variety of reasons.

-Mike
 
T

ToeNi

It might or might not mean that. Opening a file can
fail for a variety of reasons.

I agree. There are two file types in C++ (text format, binary format)
We can't open a file without knowing the file type. Append mode
opening is work like this "first open the file , second check the
existence , if exist, peek pointer is go to the end of the file, if
doesn't exist , create the file & peek pointer is ready to put data".
This is what I have learn.
Plz correct me, if I'm wroung.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top