469,570 Members | 1,717 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,570 developers. It's quick & easy.

checking if a file/stream exists


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: nb******@cyf-kr.edu.pl |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
Jul 22 '05 #1
14 4689
Leslaw Bieniasz wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
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

Jul 22 '05 #3
Shan wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4
>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.

Jul 22 '05 #5

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:41***************@gascad.at...
Shan wrote:

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.


Also a file with size zero might already exist.

-Mike
Jul 22 '05 #6

"Shan" <sh*******@rediffmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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.


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

-Mike
Jul 22 '05 #7
> 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


Jul 22 '05 #8


Hello,

On Mon, 3 Jan 2005, Karl Heinz Buchegger wrote:
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.


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: nb******@cyf-kr.edu.pl |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
Jul 22 '05 #9
Leslaw Bieniasz wrote:
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?


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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #10

Karl Heinz Buchegger wrote:
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

Jul 22 '05 #11

"Stephen Howe" <sjhoweATdialDOTpipexDOTcom> wrote in message
news:41**********************@news.dial.pipex.com. ..
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.


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
Jul 22 '05 #12
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:
for input stream

1.open the file anymode(ios::app, ios::in, ios::out)
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

Jul 22 '05 #13

"ToeNi" <to*******@ownmail.net> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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:
for input stream

1.open the file anymode(ios::app, ios::in, ios::out)
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
Jul 22 '05 #14
> 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.

Jul 22 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Geiregat Jonas | last post: by
reply views Thread by JD | last post: by
17 posts views Thread by Peter Duniho | last post: by
3 posts views Thread by tshad | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.