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.
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
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Ž
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.
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?Originally Posted by SlyMaelstrom
Thanks.
-Zack
Ah I understand, I wasn't thinking clearly. Try itsme's suggestion.
v Same goes to you down there v
Sent from my iPadŽ
>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.
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 .Originally Posted by itsme86
-Zack
Thanks anyway, bud .Originally Posted by SlyMaelstrom
-Zack
I've used stat() in Windows with several different compilers. I know it works for sure with Dev-C++:
My output: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; }
C:\ is a directory!
C:\io.sys is not a directory!
If you understand what you're doing, you're not learning anything.
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.
Nah, it's cool. I'm glad to learn anyway .Originally Posted by itsme86
-Zack
I don't think ofstream creates files that don't exist, so it should be okay.Code:#include <fstream> ofstream fout; fout.open("/internet/test", ios::out);
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.
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.
Thanks all. The stats thing worked with Borland 5.5 .
-Zack
as Daved said
>> Boost has a file system library that might also help with this if stat isn't enough.
what could be simpler?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; }
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?