简体   繁体   English

C ++目录搜索

[英]C++ directory search

So I'm trying to write a little C++ program to check whether or not a directory exists on a Windows platform (I am aware that other languages are more suited for this type of use, but I want to do it in c++). 因此,我正在尝试编写一个C ++程序,以检查Windows平台上是否存在目录(我知道其他语言更适合此类使用,但我想使用c ++来实现)。

This is what I have so far (it compiles): 这是我到目前为止(编译)的内容:

std::string DirectorySelector::SpecifyDirectory(void)
{
    std::cout << "Enter directory for file renaming: ";
    std::cin >> directory;

    if (ValidateDirectory(directory) == 1) { SpecifyDirectory(); }
    else { return directory; }
}

int DirectorySelector::ValidateDirectory(std::string directory)
{
    error = "Error 01: Directory not found.";

    std::ifstream fin (directory);
    if (!fin) 
    { 
        std::cerr << error << "\n\n"; 
        fin.close();
        return 1;
    }
    else
    {
        fin.close();
        return 2;
    }
    }

So obviously I'm currently asking for the user to input their desired directory as a string, not sure if this is a wise choice? 所以很明显,我目前正在要求用户以字符串形式输入所需的目录,不确定这是否明智?

I have done a little research into whether Windows folders (directories) have an extension, but it appears not. 我对Windows文件夹(目录)是否具有扩展名做了一些研究,但似乎没有。

I assume I am missing something obvious, such as a predefined C++ keyword to use somewhere? 我认为我缺少明显的东西,例如要在某个地方使用的预定义C ++关键字?

If any answers could be fully explained as to what is going on that would be fantastic, as I don't like to use stuff which I don't understand. 如果可以对发生的事情做出充分的解释,那就太好了,因为我不喜欢使用我不理解的东西。

Plus any tips to do with coding standards that I may not be adhering to would obviously be greatly appreciated. 加上我可能不遵守的与编码标准有关的任何技巧,将不胜感激。

Thanks in advance. 提前致谢。

If you want to use DIRENT (unix method) in windows then see here, advantage is cross platform (dirent is pretty much everywhere except windows): 如果要在Windows中使用DIRENT(unix方法),请参见此处,优点是跨平台(除Windows以外,其他地方几乎都是dirent):

http://www.softagalleria.net/dirent.php http://www.softagalleria.net/dirent.php

If you want to use the Windows API for this: 如果要为此使用Windows API:

How to check if directory exist using C++ and winAPI 如何使用C ++和WinAPI检查目录是否存在

For a portable (across many platforms) file-management system you could use boost::filesystem 对于便携式(跨许多平台)文件管理系统,可以使用boost :: filesystem

The documentation may look a bit complex for a relative beginner but they probably give you examples that will enable you to get going on what you want, and if you get stuck you can always come back here and ask specifics. 对于一个初学者来说,文档可能看起来有些复杂,但是它们可能会为您提供示例,使您能够继续进行所需的工作,如果遇到困难,可以随时返回此处并提出具体要求。

Your existing code is incorrect in its use of ifstream which is used to open a file for read-only. 您现有的代码在使用ifstream (用于打开只读文件)时使用不正确。 You cannot use this to open a directory (to list its contents or see if it exists). 您不能使用它来打开目录(列出目录或查看目录是否存在)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM