如何使用Boost库检查作为输入的字符串是否为c++中的有效目录
How to check if a string given as input is a valid directory in C++ using Boost Libraries?
我正在使用dev - c++ Ide进行编码,我想检查作为输入的字符串是否在c++中是一个有效的现有目录,我参考了StackOverflow上的这些帖子,但我找不到一个我能理解的解决方案-如何检查目录是否存在使用c++和winAPI在所有情况下都返回true所以没有使用)有人能帮助我解决这个问题吗?
您可能会发现这很有用:http://msdn.microsoft.com/en-us/library/bb773584%28VS.85%29.aspx
粘贴示例:
#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:TESTfile.txt
The file requested "C:TESTfile.txt" is a valid file
The return from function is : 1
The file requested "C:TESTfile.doc" is not a valid file
The return from function is : 0
使用Boost的便携式解决方案。文件系统:
#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
:
#include <boost/filesystem.hpp>
if ( !boost::filesystem::exists( "my_directory" ) )
{
std::cout << "Can't find my directory!" << std::endl;
}
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 检查输入是否不是整数或数字
- 是否可以初始化不可复制类型的成员变量(或基类)
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 此代码是否违反一个定义规则
- 是否需要删除包含对象的"pair"?
- 是否可以从int转换为enum类类型
- 无论条件是否为true,if总是在c++中执行
- 如何找到大小'x'数组是否完全填充,在C++?
- 检查值是否在集合p1和p2中,但不在p3中
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 检查 std::shared_ptr<> 的当前底层类型是否为 T
- 在c++中检查长方体是否尽可能快地重叠(无迭代)
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- C/C++预处理器是否可以检测一些编译器选项
- 是否可以用"iostream"包装现有的TCP/OOpenSSL会话