472,328 Members | 1,224 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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 4976
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Geiregat Jonas | last post by:
is using if(open("file",O_EXCL) != -1){ printf("File does exists")}else{printf("file does not exists"); } a good way of checking if a file...
1
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm...
18
by: Jen | last post by:
I'm using Microsoft's own VB.NET FTP Example: http://support.microsoft.com/default.aspx?scid=kb;en-us;832679 I can get the program to create...
0
by: JD | last post by:
Hello Everyone, I am trying to set up a media kit page where when a user comes to the page for the first time and click on the image they wish...
17
by: Peter Duniho | last post by:
I searched using Google, on the web and in the newsgroups, and found nothing on this topic. Hopefully that means I just don't understand what I'm...
3
by: tshad | last post by:
I have a function that downloads a file to the users computer and it works fine. The problem is that I then want the program to rename the file...
4
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my...
1
by: sandhyabhavani | last post by:
This article is used to zip a file or directory using vb.net. The classes and method to zip a file is availale in java.io, java.util, java.util.zip ...
0
by: nagashree | last post by:
Hi, i want to compress the file or directory i am able to do that can anybody help me to decompress the file when i double click on the zipped...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.