#### Archived

This topic is now archived and is closed to further replies.

# Testing for file or path

This topic is 5996 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Let''s say I have a string with a file or path in it, how can I determine which one it is? Incase you didn''t already guess this, I''m doing this in C/C++. Functions, headers, links, however you can help . Thanks in Advance. [Resist Windows XP''s Invasive Production Activation Technology!]

##### Share on other sites
Hi,

I didn't understand what you want :-). Can you elaborate :-).

Well I am shooting arrows in the dark, but you might be looking for opendir(char *name); function :-).

man opendir

BTW from the title - generally in shell scripts one would do it like test -f filename - it would test for file and test -d $PATH would test for directory. Not sure if this is what you want to do, but ofcourse this isn't c/c++. Edited by - flame_warrior on September 22, 2001 3:35:37 AM #### Share this post ##### Link to post ##### Share on other sites When you say you have a string with a path/file name in it, what exactly do you mean? (elaborate) After careful deliberation, I have come to the conclusion that Nazrix is not cool. I am sorry for any inconvienience my previous mistake may have caused. We now return you to the original programming #### Share this post ##### Link to post ##### Share on other sites Well, I think I found a way to do it using the "stat" function, but I''m not sure since I haven''t tested it. Basically, I have a string like this "/home/Someone/fish" and I don''t know whether or not the ''file'' /home/Someone/fish is a directory or a actual file. I''m trying to write a crossplatform wrapper for things like getting a list of directories and files, creating links, hiding the / (*nix), \ (DOS/Windows), or : (MacOS 9) path styles, et cetera. [Resist Windows XP''s Invasive Production Activation Technology!] #### Share this post ##### Link to post ##### Share on other sites In Unix, everything is a file. Directories, sockets, processes, pipes - they''re all files. So the distinction between a directory and a file is simply an attribute. The unix shell supports a command -d that returns true if the specified file is a directory: if( -d$file ){  //... do whatever}

Perl also supports that syntax. I don''t know which functions return file attributes, but you want to find out whether the "directory" bit is set. Worst case scenario, I think you can do
sprinf( buffer, "-d %s", file ); // correct syntax. i don''t use this functionsystem( buffer );

Type ''man system'' to find out what headers to include and what libraries to link with.

##### Share on other sites
quote:
Original post by Null and Void
Let''s say I have a string with a file or path in it, how can I determine which one it is? Incase you didn''t already guess this, I''m doing this in C/C++. Functions, headers, links, however you can help . Thanks in Advance.

[Resist Windows XP''s Invasive Production Activation Technology!]

##### Share on other sites
What''s with that last post inverting my smiley ? Anyway, the stat function did what I wanted. I finished writing the basics library for both Win32 and Linux, hopefully sourceforge lets me post it there with some really simple documentation in a few days. I''m releasing it under the ZLib license, since it is so small that I don''t care a whole lot what people do with it .

[Resist Windows XP''s Invasive Production Activation Technology!]

##### Share on other sites
I just noticed that throughout this thread I''ve been using the word "path" where I should say "directory". That explains why you didn''t understand what I was asking . I must have just been being spacey for a while, heh.

[Resist Windows XP''s Invasive Production Activation Technology!]