Thread: Determine if input is file or directory

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    54

    Determine if input is file or directory

    Hi:

    How can I determine whether an input string refers to a file or a directory?

    For example: input is /internet/test

    Is this a file or a directory? Is there a way to determine this using C++?

    Thanks.
    -Zack

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Check the string for slashes or backslashes.
    Code:
    std::string input;
    /* User Entry */
    if(input.find("/") == std::string::npos && input.find("\\") == std::string::npos) /* File is in the current directory */
    else /* File is in another directory */
    Last edited by SlyMaelstrom; 06-26-2006 at 02:30 PM.
    Sent from my iPadŽ

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You can use one of the stat() function on it and find out the type that way. For more info: http://www.die.net/doc/linux/man/man2/stat.2.html
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by SlyMaelstrom
    Check the string for slashes or backslashes.
    I'm sorry, but I don't quite understand. In Unix, the "test" could be a file or a directory. Assuming this is user-inputted, how can I (or the program) determine?

    Thanks.
    -Zack

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Ah I understand, I wasn't thinking clearly. Try itsme's suggestion.

    v Same goes to you down there v
    Sent from my iPadŽ

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Check the string for slashes or backslashes.
    How does that work? Let's say I have "/mydir/something", how do you know what "something" is when it could be either a directory or a file and the string contains slashes? There's no way to determine such a thing in every case based solely on the contents of the path.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by itsme86
    You can use one of the stat() function on it and find out the type that way. For more info: http://www.die.net/doc/linux/man/man2/stat.2.html
    Thanks for the suggestion. Is there a way to do this on Windows as well? I understand that most Windows files will have an extension, but in a mixed environment, there really can't be a guarantee .
    -Zack

  8. #8
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by SlyMaelstrom
    Ah I understand, I wasn't thinking clearly. Try itsme's suggestion.

    v Same goes to you down there v
    Thanks anyway, bud .
    -Zack

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I've used stat() in Windows with several different compilers. I know it works for sure with Dev-C++:
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    
    int main(void)
    {
      struct stat st;
    
      if(stat("C:\\", &st) == -1)
        puts("Error!");
      else
        printf("C:\\ is%s a directory!\n", S_ISDIR(st.st_mode) ? "" : " not");
    
      if(stat("C:\\io.sys", &st) == -1)
        puts("Error!");
      else
        printf("C:\\io.sys is%s a directory!\n", S_ISDIR(st.st_mode) ? "" : " not");
    
      return 0;
    }
    My output:
    C:\ is a directory!
    C:\io.sys is not a directory!
    If you understand what you're doing, you're not learning anything.

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Oops, sorry. Forgot this was the C++ side of things. Maybe someone can C++ify it for you.
    If you understand what you're doing, you're not learning anything.

  11. #11
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by itsme86
    Oops, sorry. Forgot this was the C++ side of things. Maybe someone can C++ify it for you.
    Nah, it's cool. I'm glad to learn anyway .
    -Zack

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
    #include <fstream>
    
    ofstream fout;
    fout.open("/internet/test", ios::out);
    I don't think ofstream creates files that don't exist, so it should be okay.

    If test is a directory this function will fail, so it's merely important to check the state of the stream after you open it. Most operating systems can handle files without extentions these days, so it's possible that it will work if "test" exists as a datafile.
    Last edited by whiteflags; 06-26-2006 at 03:49 PM.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Yes, ofstream will create a file that doesn't exist. That also might fail if the file is read-only.

    Boost has a file system library that might also help with this if stat isn't enough.

  14. #14
    Registered User
    Join Date
    May 2005
    Posts
    54
    Thanks all. The stats thing worked with Borland 5.5 .
    -Zack

  15. #15
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    as Daved said
    >> Boost has a file system library that might also help with this if stat isn't enough.

    Code:
    #include "boost/filesystem/operations.hpp" // includes boost/filesystem/path.hpp
    #include "boost/filesystem/fstream.hpp"    // ditto
    #include <iostream>                        // for std::cout
    using boost::filesystem;                   // for ease of tutorial presentation;
    
    path my_path( "/internet/test" );
    if (is_directory(my_path))
    {
        std::cout << "it's a directory!" << std::endl;
    }
    what could be simpler?

    edit: oh and it's portable too!
    Last edited by ChaosEngine; 06-26-2006 at 06:15 PM.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 02:54 AM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM