473,668 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Good way of checking if file exists ?

is using
if(open("file", O_EXCL) != -1){ printf("File does
exists")}else{p rintf("file does not exists"); }

a good way of checking if a file exists or not, if not how should I do it ?

Nov 14 '05 #1
15 114202
a standard C compatible way could be:

FILE *fp = fopen("file","r ");
if( fp ) {
// exists
fclose(fp);
} else {
// doesnt exist
}


Geiregat Jonas wrote:
is using
if(open("file", O_EXCL) != -1){ printf("File does
exists")}else{p rintf("file does not exists"); }

a good way of checking if a file exists or not, if not how should I do it ?

Nov 14 '05 #2
Robert Frunzke <Ro************ **@freenet.de> scribbled the following:
a standard C compatible way could be: FILE *fp = fopen("file","r ");
if( fp ) {
// exists
fclose(fp);
} else {
// doesnt exist
Doesn't exist, exists but isn't readable, exists but already has too
many locks, or other similar reasons.
}


It's impossible to check existence for certain in pure ISO standard C.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Parthenogeneti c procreation in humans will result in the founding of a new
religion."
- John Nordberg
Nov 14 '05 #3
Geiregat Jonas <en***@sdf-eu.org> wrote:
is using
if(open("file", O_EXCL) != -1){ printf("File does
exists")}else{p rintf("file does not exists"); }

a good way of checking if a file exists or not, if not how should I do it ?


I would recommend using 'fopen' instead to use a standard c function.

However, standard C doesn't provide any guaranteed way to determine
whether a file exists or not. fopen can fail even if a file does exist.

However, it is possible that it is a reasonable method for your system,
but that question could only be answered by people knowledgeable with
your specific system.

--
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===
Nov 14 '05 #4
Joona I Palaste wrote:
Robert Frunzke <Ro************ **@freenet.de> scribbled the following:
a standard C compatible way could be:


FILE *fp = fopen("file","r ");
if( fp ) {
// exists
fclose(fp);
} else {
// doesnt exist

Doesn't exist, exists but isn't readable, exists but already has too
many locks, or other similar reasons.

}

It's impossible to check existence for certain in pure ISO standard C.

Using the stat function would solve some probleme's because you don't
need any access rights. I think stat is the best way (I'm using linux).

Nov 14 '05 #5
Op Wed, 07 Jan 2004 22:35:17 +0000 schreef Geiregat Jonas
<en***@sdf-eu.org>:
Joona I Palaste wrote:
Robert Frunzke <Ro************ **@freenet.de> scribbled the following:
a standard C compatible way could be:


FILE *fp = fopen("file","r ");
if( fp ) {
// exists
fclose(fp);
} else {
// doesnt exist

Doesn't exist, exists but isn't readable, exists but already has too
many locks, or other similar reasons.

}

It's impossible to check existence for certain in pure ISO standard C.

Using the stat function would solve some probleme's because you don't
need any access rights. I think stat is the best way (I'm using linux).


access rights and stat are unknown to pure ISO standard C.

--
Coos
Nov 14 '05 #6
Geiregat Jonas <en***@sdf-eu.org> writes:
is using
if(open("file", O_EXCL) != -1){ printf("File does
exists")}else{p rintf("file does not exists"); }

a good way of checking if a file exists or not, if not how should I do it ?


There's no really good portable way to determine whether a named file
exists; you'll probably have to resort to system-specific methods
(which you can ask about in another newsgroup).

Before you do, you should define more clearly what information you're
looking for and what you intend to do with it. On a multi-processing
system, for example, if your logic is something like this:

if (file exists) {
open existing file
}
else {
create new file
initialize new file
}

you should allow for the possibility that the file is created <OT>by
another process</OT> after you check for its existence and before you
attempt to create it.

Note also that if a file doesn't exist, that doesn't imply that you
have permission to create it.

In many cases, it may not be possible to determine whether a file
exists. On a system with user accounts and directories with
permission settings, an attempt to determine whether a file exists may
fail because you don't have permission to read the directory that may
or may not contain it.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #7
Geiregat Jonas <en***@sdf-eu.org> wrote in message news:<3f******* *************** @news.skynet.be >...
is using
if(open("file", O_EXCL) != -1){ printf("File does
exists")}else{p rintf("file does not exists"); }

a good way of checking if a file exists or not, if not how should I do it ?


In my opinion, the best option is to use access() call instead of
open() call. In this case, the above given code will look like:

if (access("file", F_OK) == 0)
{
printf("Files does exists");
}
else
{
printf("File does not exist");
...
}

Regards,
- Rahul Agarkar
Nov 14 '05 #8
Rahul Agarkar <ra***********@ hotmail.com> scribbled the following:
Geiregat Jonas <en***@sdf-eu.org> wrote in message news:<3f******* *************** @news.skynet.be >...
is using
if(open("file", O_EXCL) != -1){ printf("File does
exists")}else{p rintf("file does not exists"); }

a good way of checking if a file exists or not, if not how should I do it ?
In my opinion, the best option is to use access() call instead of
open() call. In this case, the above given code will look like: if (access("file", F_OK) == 0)
{
printf("Files does exists");
}
else
{
printf("File does not exist");
...
}


Neither access() or open() are ISO standard C functions. Whatever next,
will you be suggesting OpenFile() in <windows.h> or something?
(That function name might be wrong. I haven't actually *looked* at
<windows.h>.)

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"This isn't right. This isn't even wrong."
- Wolfgang Pauli
Nov 14 '05 #9
Rahul Agarkar wrote:
Geiregat Jonas <en***@sdf-eu.org> wrote in message
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 exists or not, if not how
should I do it ?


In my opinion, the best option is to use access() call instead of
open() call. In this case, the above given code will look like:

if (access("file", F_OK) == 0)
printf("Files does exists");
else {
printf("File does not exist");
...
}


(slightly reformatted). open, access, O_EXCL and F_OK do not
exist in standard C, the subject of this newsgroup. Please do not
give off-topic answers here, where there may well be nobody to
correct any errors.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
6020
by: Alex Chan | last post by:
Hi Group, I encountered something strange with File.Exists(string path). This function is simply to check whether a file exists in certain path. However, if the path starts with "file:\" in the begining of the string, no matter the file exists or not. it will return false without any exception. It is expected? Regards, Alex
6
4236
by: Bas | last post by:
I'm using If File.Exists("c:\docs\myfile.txt") msgbox("Found") End This work fine on machine with Vb.Net installed but NOT on regular workstations ie. when deployed!! Any ideas?
10
14376
by: Ricardo Luceac | last post by:
Hi all. I'm having a problem with this, I have look if a file exists, if don't wait till it is created and if it exists I need to open it. I do the following: for (; ; ) {
2
538
by: PiotrKolodziej | last post by:
Hi I want to check if the file can be executed. Its directory path is either in environment 'path' variable or isn't there. Exists method seems like it's not checking variables at all. For any help thanks.
2
1312
by: =?Utf-8?B?R29yZG9u?= | last post by:
Hi; What is the correct syntax for checking for a file's existence based on it's extension ? Example: Imports system.io Dim path as string = "C:\myDirectory\*.txt"
26
4938
by: Army1987 | last post by:
Is this a good way to check wheter a file already exists? #include <stdio.h> #include <stdlib.h> int ask(const char *prompt); typedef char filename; int main(int argc, char *argv) { FILE *in, *out;
13
24181
by: darkslide | last post by:
Hi, I'm checking if a file exists with the following code; If System.IO.File.Exists("C:\test.txt") = True Then MsgBox("File Exists") Else MsgBox("File Does Not Exist") End If For some reason it always returns false even though the file does exist. Any suggestions? Could this be a security issue?
7
19080
by: sprash | last post by:
Newbie question: I'm trying to determine if a file physically exists regardless of the permissions on it Using File.Exists() returns false if it physically exists but the process does not have the necessary permissions. One hack could be to check for length and that would throw a FileNotFoundException ...but there is got to be a better way!
1
3217
by: timexsinclair2068 | last post by:
This is a very simple question. Is there a simple,short way of checking if the application's App.Config file exists? Thanks!
0
8462
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8382
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8586
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2792
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1787
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.