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: -
File.Exists(strDir & "filename.txt") 'returns a boolean true if it exists, false if not
-
Thanks,
Sitko.
10 2285
Hi,
I found this online: -
bool file_exists(const char * filename)
-
{
-
if (FILE * file == fopen(filename, "r"))
-
{
-
fclose(file);
-
return true;
-
}
-
return false;
-
}
-
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? -
int file_exists( char * filename)
-
{
-
if (FILE * file == fopen(filename, "r"))
-
{
-
fclose(file);
-
return 1;
-
}
-
else
-
{
-
return 0;
-
}
-
}
-
Thanks,
Sitko
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.
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.
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 -
if (FILE * file == fopen(filename, "r"))
-
when you changed that? Hint - what is fopen()? Does it have a return type?
Well, let's think about that. You changed the conditional 'is equal to' == operator to the assignment '=' operator. So what happened to -
if (FILE * file == fopen(filename, "r"))
-
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.
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...
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.
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?)
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 ?
|
by: StGo |
last post by:
How can i read/write file's custom attributs(like subject,author...) in
C#???
Thanks :))
|
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...
|
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
|
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."
| |
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
|
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
|
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...
|
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!
|
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!
|
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...
| |
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,...
|
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |