I am using Dev-C++ Ide for coding and I want to check if a string given as input is a valid existing directory or not in C++, I referred to these posts on StackOverflow but I couldn't find a solution that I could understand - How to check if directory exist using C++ and winAPI one returns true in all case so no use) can anyone help me out with this problem?
Asked
Active
Viewed 2,388 times
-1
-
Give code, expected output, your actual output. Also, depending on what version of Dev-C++ you have installed, it may be severely outdated. – Aug 21 '14 at 17:32
-
Are you suggesting that a winapi call does not work? Sounds a little unlikely, given the amount of testing it has had... – Martin James Aug 21 '14 at 17:32
-
`I couldn't find a solution that I could understand ` So how are we to know what you understand? The solutions given at that link were very simple, especially the one that got a rep of 30. – PaulMcKenzie Aug 21 '14 at 17:34
-
I am new to using this so can you like tell me given the input as a string parameter how to tell if its a directory or not? – AnkitSablok Aug 21 '14 at 17:35
-
1@AnkitSablok - The solution is right there on the page you have a link to. It is the very first answer -- it didn't get 30 upvotes for nothing. – PaulMcKenzie Aug 21 '14 at 17:36
3 Answers
0
You may find this useful : http://msdn.microsoft.com/en-us/library/bb773584%28VS.85%29.aspx
Pasting example :
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main(void)
{
// Valid file path name (file is there).
char buffer_1[ ] = "C:\\TEST\\file.txt";
char *lpStr1;
lpStr1 = buffer_1;
// Invalid file path name (file is not there).
char buffer_2[ ] = "C:\\TEST\\file.doc";
char *lpStr2;
lpStr2 = buffer_2;
// Return value from "PathFileExists".
int retval;
// Search for the presence of a file with a true result.
retval = PathFileExists(lpStr1);
if(retval == 1)
{
cout << "Search for the file path of : " << lpStr1 << endl;
cout << "The file requested \"" << lpStr1 << "\" is a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}
else
{
cout << "\nThe file requested " << lpStr1 << " is not a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}
// Search for the presence of a file with a false result.
retval = PathFileExists(lpStr2);
if(retval == 1)
{
cout << "\nThe file requested " << lpStr2 << "is a valid file" << endl;
cout << "Search for the file path of : " << lpStr2 << endl;
cout << "The return from function is : " << retval << endl;
}
else
{
cout << "\nThe file requested \"" << lpStr2 << "\" is not a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}
}
OUTPUT
==============
Search for the file path of : C:\TEST\file.txt
The file requested "C:\TEST\file.txt" is a valid file
The return from function is : 1
The file requested "C:\TEST\file.doc" is not a valid file
The return from function is : 0

Madhavan Malolan
- 719
- 6
- 24
0
Portable solution using Boost.Filesystem:
#include <boost/filesystem.hpp>
//...
boost::filesystem::path dir(directory_path_string);
if (boost::filesystem::is_directory(dir) && boost::filesystem::exists(dir))
{
// directory exists
}

Piotr Skotnicki
- 46,953
- 7
- 118
- 160
-
okk yeah, I have installed boost, when I try to #include
it reports an error saying there is no such file can you point me to some reference which tells me how to use boost in dev c++? – AnkitSablok Aug 21 '14 at 17:39 -
-
2@AnkitSablok Strange you said you were new to this in your earlier comment, but seem to be able to want to use boost. – PaulMcKenzie Aug 21 '14 at 17:42
-
Menu bar -> Tools -> Compiler options -> Directories -> C++ Header files: Add path you the main directory of Boost – Piotr Skotnicki Aug 21 '14 at 17:42
-
I am new to using Boost actually it is my first time :), so thats why I want to do it I checked out in earlier stack overflow posts regarding Boost – AnkitSablok Aug 21 '14 at 17:43
-
-
-
@AnkitSablok Then you should reword your original question to use `boost` only. Otherwise, the solution you're looking for is a simple call to `GetFileAttributesA` and checking the "directory bit" on the return value. It is the same solution given to you on the page you linked to in your question. – PaulMcKenzie Aug 21 '14 at 17:47
-
-
@Ankit For information on how to build boost on your platform, refer to the [documentation](http://www.boost.org/doc/libs/1_56_0/more/getting_started/index.html). For a live example that demonstrates this working code, see [this](http://coliru.stacked-crooked.com/a/9743a25c3728b98c). Remember to link against `boost_filesystem` and `boost_system` which must be built before you can use them. – Aug 21 '14 at 17:53
-
0
Try boost::filesystem
:
#include <boost/filesystem.hpp>
if ( !boost::filesystem::exists( "my_directory" ) )
{
std::cout << "Can't find my directory!" << std::endl;
}

Scott Yang
- 339
- 3
- 6