Private Messages Options Search Blogs Images Chat Cam Portals Calendar FAQ's Join  
Asylum Forums : Powered by vBulletin version 2.2.8 Asylum Forums > WIT - Whore Institute of Technology > Coding help
  Last Thread   Next Thread
Author
Thread [new thread]    [post reply]
Talarohk
The Pedanticator

Registered: Feb 2003
Location: Oceanside, CA
Posts: 5073

Coding help

Can anyone tell me if there is a standard function I could use in C code to determine whether a named folder exists or not? I know I can do it in shell, but I'd like to do it in C.

Does fopen set errno to something useful if the attempted open is a preexisting folder?

Thankee.

__________________

Report this post to a moderator | IP: Logged

Old Post 02-28-2006 12:34 AM
Talarohk is offline Click Here to See the Profile for Talarohk Click here to Send Talarohk a Private Message Visit Talarohk's homepage! Find more posts by Talarohk Add Talarohk to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

You'll return NULL if you try to fopen a file for reading that doesn't exist (don't try to open it for writing, or it'll create one if it doesn't exist, or nuke the existing one if it does). I think that you can check for NULL in C, if I recall aright.

NULL is returned if there is any error, however, not just a 'no file of that name exists' error.

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of

Report this post to a moderator | IP: Logged

Old Post 02-28-2006 12:45 AM
Smug Git is online now Click Here to See the Profile for Smug Git Click here to Send Smug Git a Private Message Find more posts by Smug Git Add Smug Git to your buddy list [P] Edit/Delete Message Reply w/Quote
Talarohk
The Pedanticator

Registered: Feb 2003
Location: Oceanside, CA
Posts: 5073

I specifically want to check for whether a given *folder* exists, though. Ideally, I'd like a function which returned different values if you handed it a path to an existing file, a path to a nonexisting file, a path to an existing directory, and a path to a nonesxisting directiory.

Actually, what *does* fopen do if you hand it a path to an existing folder?

__________________

Report this post to a moderator | IP: Logged

Old Post 02-28-2006 12:50 AM
Talarohk is offline Click Here to See the Profile for Talarohk Click here to Send Talarohk a Private Message Visit Talarohk's homepage! Find more posts by Talarohk Add Talarohk to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

Don't recall much errorhandling from C, but fopen might be written so that you can check against the error to find out what the cause was, specifically.

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of

Report this post to a moderator | IP: Logged

Old Post 02-28-2006 12:51 AM
Smug Git is online now Click Here to See the Profile for Smug Git Click here to Send Smug Git a Private Message Find more posts by Smug Git Add Smug Git to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

Ah, sorry, read that as 'file'.

fopen probably throws some special 'this isn't a file, you moron' error. Or perhaps just returns a pointer to 'moron'.

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of

Report this post to a moderator | IP: Logged

Old Post 02-28-2006 12:52 AM
Smug Git is online now Click Here to See the Profile for Smug Git Click here to Send Smug Git a Private Message Find more posts by Smug Git Add Smug Git to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

There has to be a better way of doing this, but if you ask it to open a nonexistent file (qwertyuiop.asd, say) in a folder of whose existence you're not sure, maybe it throws a different error to the 'can't find file' one (given that it must check along the location string from left to right).

I haven't written C in years and years.

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of

Report this post to a moderator | IP: Logged

Old Post 02-28-2006 12:54 AM
Smug Git is online now Click Here to See the Profile for Smug Git Click here to Send Smug Git a Private Message Find more posts by Smug Git Add Smug Git to your buddy list [P] Edit/Delete Message Reply w/Quote
Talarohk
The Pedanticator

Registered: Feb 2003
Location: Oceanside, CA
Posts: 5073

I think you're right about it setting errno based on what the problem was, but I'm having trouble determining if "this is a folder" is one of the errors.

#define moron TALAROHK

__________________

Report this post to a moderator | IP: Logged

Old Post 02-28-2006 12:55 AM
Talarohk is offline Click Here to See the Profile for Talarohk Click here to Send Talarohk a Private Message Visit Talarohk's homepage! Find more posts by Talarohk Add Talarohk to your buddy list [P] Edit/Delete Message Reply w/Quote
Talarohk
The Pedanticator

Registered: Feb 2003
Location: Oceanside, CA
Posts: 5073

Hmm. That does seem roundabout, as well as leaving little files around if the folder DOES exist, but I guess it would work in a pinch.

__________________

Report this post to a moderator | IP: Logged

Old Post 02-28-2006 12:56 AM
Talarohk is offline Click Here to See the Profile for Talarohk Click here to Send Talarohk a Private Message Visit Talarohk's homepage! Find more posts by Talarohk Add Talarohk to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

If you try to open to read, it won't create a file if none exists. Only if you open to write or append, I think.

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of

Report this post to a moderator | IP: Logged

Old Post 02-28-2006 12:57 AM
Smug Git is online now Click Here to See the Profile for Smug Git Click here to Send Smug Git a Private Message Find more posts by Smug Git Add Smug Git to your buddy list [P] Edit/Delete Message Reply w/Quote
Talarohk
The Pedanticator

Registered: Feb 2003
Location: Oceanside, CA
Posts: 5073

You're right.
Upon further looking, fopen might set errno to EISDIR if the named path is a directory and you're trying to write. Not sure what happens if you're trying to read.

__________________

Report this post to a moderator | IP: Logged

Old Post 02-28-2006 01:01 AM
Talarohk is offline Click Here to See the Profile for Talarohk Click here to Send Talarohk a Private Message Visit Talarohk's homepage! Find more posts by Talarohk Add Talarohk to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

It might be that stat is your friend in this. Not anything I've used, though.

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of

Report this post to a moderator | IP: Logged

Old Post 02-28-2006 01:08 AM
Smug Git is online now Click Here to See the Profile for Smug Git Click here to Send Smug Git a Private Message Find more posts by Smug Git Add Smug Git to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

In general, chapter 8 in Kernighan and Richie is about the Unix interface. There's a function called opendir, too

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of

Report this post to a moderator | IP: Logged

Old Post 02-28-2006 01:13 AM
Smug Git is online now Click Here to See the Profile for Smug Git Click here to Send Smug Git a Private Message Find more posts by Smug Git Add Smug Git to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

Let's be clear, I know fucking nothing about this, really.

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of

Report this post to a moderator | IP: Logged

Old Post 02-28-2006 01:29 AM
Smug Git is online now Click Here to See the Profile for Smug Git Click here to Send Smug Git a Private Message Find more posts by Smug Git Add Smug Git to your buddy list [P] Edit/Delete Message Reply w/Quote
Talarohk
The Pedanticator

Registered: Feb 2003
Location: Oceanside, CA
Posts: 5073

Opendir seems to return null for nonexistent directories, and non-null for existing ones. Perfect.
Thanks, Smug!

__________________

Report this post to a moderator | IP: Logged

Old Post 02-28-2006 01:31 AM
Talarohk is offline Click Here to See the Profile for Talarohk Click here to Send Talarohk a Private Message Visit Talarohk's homepage! Find more posts by Talarohk Add Talarohk to your buddy list [P] Edit/Delete Message Reply w/Quote
zim
-

Registered: Dec 2002
Location:
Posts: 3063

I am not a coder:

code:
#include <sys/stat.h> int isdirectory(char *path) { struct stat statbuf; if (stat(path, &statbuf) == -1) return 0; else return S_ISDIR(statbuf.st_mode); }

isdirectory returns true if the param it's given is the path to a directory, ie:
code:
#include <sys/stat.h> #include <stdio.h> int isdirectory(char *path); int main() { if (isdirectory("./")) printf("Yes\n"); else printf("No\n"); if (isdirectory("boob")) printf("Yes\n"); else printf("No\n"); } ---snip--

gives the output:
code:
Yes No


Because ./ is always the CWD, whereas I do not have a directory named boob either in / or in the CWD.

__________________
insert witty remark

Last edited by CHiPsJr on 11-09-2006 at 08:23 AM

Last edited by zim on 02-28-2006 at 01:55 AM

Report this post to a moderator | IP: Logged

Old Post 02-28-2006 01:50 AM
zim is offline Click Here to See the Profile for zim Click here to Send zim a Private Message Find more posts by zim Add zim to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

http://www.scit.wlv.ac.uk/~jphb/spo...calls/stat.html the stat() example there does the same sort of thing, from the command line.

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of

Report this post to a moderator | IP: Logged

Old Post 02-28-2006 02:01 AM
Smug Git is online now Click Here to See the Profile for Smug Git Click here to Send Smug Git a Private Message Find more posts by Smug Git Add Smug Git to your buddy list [P] Edit/Delete Message Reply w/Quote
ignatz mouse
Mistress of Futility

Registered: Mar 2004
Location: Still Orygun
Posts: 10243

filthy (sexy) nerds.

*PS. Just assume I took a wrong turn at the Gonad Forum.

Report this post to a moderator | IP: Logged

Old Post 02-28-2006 02:05 AM
ignatz mouse is offline Click Here to See the Profile for ignatz mouse Click here to Send ignatz mouse a Private Message Find more posts by ignatz mouse Add ignatz mouse to your buddy list [P] Edit/Delete Message Reply w/Quote
Talarohk
The Pedanticator

Registered: Feb 2003
Location: Oceanside, CA
Posts: 5073

That sounds like it would work, too. Many thanks, all.

Ignatz--my doctoral advisor's wife (who was also a professor, with the next office down the hall) had a serious thing for geeks. Anytime she came into the room and he was forearm-deep in eviscerating a computer, or building some sort of MRI doowhatsit, or coding loudly and vigorously, she would get this amazing look on her face. It was beautiful.

__________________

Report this post to a moderator | IP: Logged

Old Post 02-28-2006 08:16 AM
Talarohk is offline Click Here to See the Profile for Talarohk Click here to Send Talarohk a Private Message Visit Talarohk's homepage! Find more posts by Talarohk Add Talarohk to your buddy list [P] Edit/Delete Message Reply w/Quote
Thimbles worth of opinion
Symetrically challenged

Registered: Aug 2000
Location:
Posts: 7680

"man 2 open"

int open(const char *path, int flags, mode_t mode);

DESCRIPTION

The file name specified by path is opened for reading and/or writing as
specified by the argument flags and the file descriptor returned to the
calling process. The flags argument may indicate the file is to be cre-
ated if it does not exist (by specifying the O_CREAT flag), in which case
the file is created with mode mode as described in chmod(2) and modified
by the process' umask value (see umask(2)).

The flags specified are formed by or'ing the following values

O_RDONLY open for reading only...

ERRORS

[EISDIR] The named file is a directory, and the arguments specify it is to be opened for writing.

Base your switch statement off of errno.

for further info look up fcntl.h

and don't forget to close(int fd); the file descriptor returned if it's != -1.

__________________
My nipples are asymetrical... and that's a feature not a bug.

Last edited by Thimbles worth of opinion on 02-28-2006 at 08:36 AM

Report this post to a moderator | IP: Logged

Old Post 02-28-2006 08:29 AM
Thimbles worth of opinion is offline Click Here to See the Profile for Thimbles worth of opinion Click here to Send Thimbles worth of opinion a Private Message Find more posts by Thimbles worth of opinion Add Thimbles worth of opinion to your buddy list [P] Edit/Delete Message Reply w/Quote
macker
Holy Me-el

Registered: Nov 2000
Location: UK
Posts: 4736

opendir(and related functions) aren't part of the ANSI standard(although most Unices these days seem to include dirent.h so it might not matter much) so if you want to behave yourself I'd use zim's stat method.

If you plan on creating the directory if it doesn't exist, I'd just skip the stat call and just use open() directly(as there's a vague possibility that between the stat() and open() calls the directory could be created by something else. Unlikely with a local filesystem but with network filesystems that use attribute cacheing your data could be several thousand ms stale). It'll barf in a non-spectacular manner which you can catch if the directory already exists.

__________________
Expecting people to be smart team players is like looking for double Ds in an oriental brothel.

Report this post to a moderator | IP: Logged

Old Post 02-28-2006 08:50 AM
macker is online now Click Here to See the Profile for macker Click here to Send macker a Private Message Visit macker's homepage! Find more posts by macker Add macker to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

Or you'd write opendir yourself, if you didn't have it and wanted to use it. It's small.

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of

Report this post to a moderator | IP: Logged

Old Post 02-28-2006 11:23 AM
Smug Git is online now Click Here to See the Profile for Smug Git Click here to Send Smug Git a Private Message Find more posts by Smug Git Add Smug Git to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

<sys/stat.h> isn't in the ANSI standard, itself, it seems, anyhow.

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of

Report this post to a moderator | IP: Logged

Old Post 02-28-2006 11:38 AM
Smug Git is online now Click Here to See the Profile for Smug Git Click here to Send Smug Git a Private Message Find more posts by Smug Git Add Smug Git to your buddy list [P] Edit/Delete Message Reply w/Quote
macker
Holy Me-el

Registered: Nov 2000
Location: UK
Posts: 4736

But it's part of POSIX, as is dirent.h(which contains opendir), so I guess if you're going to use stat, you may as well use opendir.

My advice? Use perl. Perl doesn't suffer from this sillyness.

__________________
Expecting people to be smart team players is like looking for double Ds in an oriental brothel.

Report this post to a moderator | IP: Logged

Old Post 02-28-2006 12:56 PM
macker is online now Click Here to See the Profile for macker Click here to Send macker a Private Message Visit macker's homepage! Find more posts by macker Add macker to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

Use C#.

Embrace the beast.

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of