473,782 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

File.exists in C?

140 New Member
Hi,

I'm a VB programmer, and was wondering, is there a way to check and see if a file exists in straight C?

In VB the code is:
Expand|Select|Wrap|Line Numbers
  1. File.Exists(strDir & "filename.txt") 'returns a boolean true if it exists, false if not
  2.  
Thanks,
Sitko.
Nov 1 '07 #1
10 2285
sitko
140 New Member
Hi,

I found this online:
Expand|Select|Wrap|Line Numbers
  1. bool file_exists(const char * filename)
  2. {
  3.     if (FILE * file == fopen(filename, "r"))
  4.     {
  5.         fclose(file);
  6.         return true;
  7.     }
  8.     return false;
  9. }
  10.  
I didn't think bool existed in C, nor the usage of "true" and "false"

Is this a better version (to make it usable by all flavors of Unix?
Expand|Select|Wrap|Line Numbers
  1. int file_exists( char * filename)
  2. {
  3.     if (FILE * file =fopen(filename, "r"))
  4.     {
  5.         fclose(file);
  6.         return 1;
  7.     }
  8.         else
  9.     {
  10.         return 0;
  11.     }
  12. }
  13.  
Thanks,
Sitko
Nov 1 '07 #2
sicarie
4,677 Recognized Expert Moderator Specialist
Well, if you think about it, they do the same thing. 0 stands for a fail and any other number stands for a pass/true condition in a loop, so you're really just changing the datatype you're returning. I can't think of a specific reason one would work better than the other in Linux.
Nov 1 '07 #3
sitko
140 New Member
Well, if you think about it, they do the same thing. 0 stands for a fail and any other number stands for a pass/true condition in a loop, so you're really just changing the datatype you're returning. I can't think of a specific reason one would work better than the other in Linux.
I changed the "="s to "=="s, is that correct as well?

Thanks,
Sitko.
Nov 1 '07 #4
sicarie
4,677 Recognized Expert Moderator Specialist
I changed the "="s to "=="s, is that correct as well?

Thanks,
Sitko.
Well, let's think about that. You changed the conditional 'is equal to' == operator to the assignment '=' operator. So what happened to
Expand|Select|Wrap|Line Numbers
  1. if (FILE * file == fopen(filename, "r"))
  2.  
when you changed that? Hint - what is fopen()? Does it have a return type?
Nov 1 '07 #5
sitko
140 New Member
Well, let's think about that. You changed the conditional 'is equal to' == operator to the assignment '=' operator. So what happened to
Expand|Select|Wrap|Line Numbers
  1. if (FILE * file == fopen(filename, "r"))
  2.  
when you changed that? Hint - what is fopen()? Does it have a return type?
Yeah, I realized that it was bad to change that...and have set it back...

Now, I'm wondering is there a way to check if a directory exists?

Thanks,
Sitko.
Nov 1 '07 #6
sicarie
4,677 Recognized Expert Moderator Specialist
Just to clarify, you're doing this all in Linux, correct?

There are other ways to do this in Windows if you're using the Win32 API...
Nov 1 '07 #7
sitko
140 New Member
Yes, linux or another flavor of unix...trying convert some VB code to be able to run on Unix and Linux...

My current thinking (regarding checking to see if a directory exists), is to just make "system(); calls, with 'cd <dirName> as the argument...


Thanks,
Sitko.
Nov 1 '07 #8
sicarie
4,677 Recognized Expert Moderator Specialist
So I just did a really quick search (as I'm on my way out the door) - there is an exists() function for looking for files and directories in Linux, but I'm not sure what library it's in. But you can Google that, and if I get a chance later, I'll hop on and look. (Anyone else out there know of exists() in Linux and what lib it's in?)
Nov 1 '07 #9
sitko
140 New Member
I attempted to do a "man exists" on our unix and it says "no manual entry for exists"

Ideally, I want to make this as platform independent as possible...port ability is the goal here...

Thanks,
Sitko.
Nov 1 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

15
114213
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 exists or not, if not how should I do it ?
3
26269
by: StGo | last post by:
How can i read/write file's custom attributs(like subject,author...) in C#??? Thanks :))
18
3183
by: Dan | last post by:
I have code like the following to test for existence of a file. I know the file is there, but File.Exists returns FALSE. The problem appears to be that the file is in a directory beneath "My Documents". When I move it to a directory directly under the root, File.Exists returns TRUE. So I tried using FileIOPermission to give me rights to read it, but still no luck. Any suggestions? Thanks... FileIOPermission f = new...
2
9390
by: Zeno Lee | last post by:
I'm using File.Exists to test a file on my C: drive. My program was strongly named and had caspol -af run on it to allow it to run from the network. There are 3 ways I am doing this: 1) Run from my C: drive, File.Exists works properly and says C:\Temp\test.txt exists 2) Run from my H:\ (network) drive, File.Exists says the file doesn't exist. 3) Run from Visual Studio debugger, with project configuration property
2
14409
by: Chris Fink | last post by:
I am using the System.IO.File class to determine if a file exists on a network share. The File.Exists method keeps returning false, even though the file does exist. The MSDN documentation states, "If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns false regardless of the existence of path."
4
2319
by: DEWright_CA | last post by:
I am trying to see if a file exists in a virtual directory, and if so run a method. I try doing File.Exists and the method runs but the file isn't there. Is there a web version of File.Exists or some otherway to get this type of functionality without too much heartache??? Thanks -- D @ premierdata
52
7545
by: paytam | last post by:
Hi all Can anyone tell me how can I check that a file exist or no.I mean when you use this commands FILE *fp; if(!fp) //Could not open the file doen't show why it can not open it,may be the file doesn't exist.Now tell me what should I do! Thanks
17
8029
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 supposed to be doing here. :) The problem: I am trying to use the SaveFileDialog class to get a filename, which is subsequently opened for writing (write access, read sharing, but using read/write sharing doesn't make the problem go away anyway). Sometimes, on the statement where I...
12
4572
by: snow | last post by:
Hi All, I noticed if file path has a white space, for example "C:\my document \test.txt", the function File.Exists(filePath) always return false in release mode. How could I make this function work for the file which is located in a direcotry with a white space? Thanks!
7
19117
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!
0
9639
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...
1
10080
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
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7492
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6733
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
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
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
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
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.