How to check if a string given as input is a valid directory in C++ using Boost Libraries?

c check if directory exists and create
how to check if a path is valid in c
c check if directory exists
c check directory permissions
c check if command exists
c++ check if file exists
how to give file path in c program
c program to search for a file in a directory

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?

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

C++ : Check if given path is a file or directory using Boost & C++17 , For Boost Library, use the same function , with following header file and namespace i.e. Given a string str, the task is to check if the string is a valid identifier or not. In order to qualify as a valid identifier, the string must satisfy the following conditions: It must start with either underscore(_) or any of the characters from the ranges [‘a’, ‘z’] and [‘A’, ‘Z’]. There must not be any white space in the string.

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
}

Boost Filesystem path.hpp Header, Filesystem Library functions traffic in objects of class path, provided by this header. Rationale: If Filesystem functions trafficked in std::strings or C-style strings, a directory path string using the operating system's format, and a file path string best constructed with the native name check, so that the user input follows the  Write a function to check whether a given input is an integer or a string. Definition of an integer : Every element should be a valid digit, i.e '0-9'.

Try boost::filesystem:

#include <boost/filesystem.hpp>

if ( !boost::filesystem::exists( "my_directory" ) )
{
  std::cout << "Can't find my directory!" << std::endl;
}

Boost Filesystem Library, The Filesystem Library supplies several headers, all in directory boost/filesystem: Validity checking functions are also provided. the following code snippet take const path& arguments, the user can just code C-style strings: It provides input iterators over the contents of a directory, with the value type being class path. sys/stat.h header file defines a macro S_ISDIR(), used to check directory existence. The macro accepts stat.st_mode param and returns a non-zero integer if given file is a directory, otherwise zero. Program to check directory existence /** * C program to check whether a directory exists or not.

Chapter 35. Boost.Filesystem - Paths, boost::filesystem::path can be initialized with wide strings. Wide strings are of these string types. None of the constructors of boost::filesystem::path validate paths or check whether the given file or directory exists. When run on Windows, Example 35.3 writes C:\Windows\System to the standard output stream three times. 14 thoughts on “ How to validate numeric-integer input in C ” Chuck March 10, 2013 at 3:42 pm. Instead of checking for what you could do is read in that character and not use it.

Boost Filesystem Library, The Boost Filesystem Library provides portable facilities to query and manipulate paths Validity checking functions are also provided. following code snippet take const path& arguments, the user can just code C-style strings: It provides input iterators over the contents of a directory, with the value type being class path. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more How to check input is a valid integer [duplicate]

Windows check if directory exists c, In a Windows C application I want to validate a parameter passed into a function to I'm trying to run a check to see if a folder exists in windows based on a string input. The function to create a new directory is not part of the C or C++ standard library. Check if given path exists or not using boost::filesystem::exists() API. I am writing boilerplate that handles command line arguments that will later be passed to another function. This other function will handle all of the directory creation (if necessary). Therefore my bp only needs to check if an input string could be a valid directory, OR a valid file, OR (some other thing).

Comments
  • Give code, expected output, your actual output. Also, depending on what version of Dev-C++ you have installed, it may be severely outdated.
  • Are you suggesting that a winapi call does not work? Sounds a little unlikely, given the amount of testing it has had...
  • 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.
  • 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 - 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.
  • okk yeah, I have installed boost, when I try to #include <boost/filesystem.hpp> 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 You need to point Dev-C++ to your boost includes.
  • @AnkitSablok Strange you said you were new to this in your earlier comment, but seem to be able to want to use boost.
  • Menu bar -> Tools -> Compiler options -> Directories -> C++ Header files: Add path you the main directory of Boost
  • 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