替换 findfirst() 和 findnext()

发布于 2024-11-18 11:16:36 字数 251 浏览 4 评论 0原文

是否有 findfirst()findnext() 的替代品。 我使用的是 microsoft Visual C++ 2010 express,它不支持这些函数,头文件 也不支持?

我想使用这些函数来计算目录中的文件数量,但是没有这些函数我遇到了问题。

如果没有替代上述功能还有其他办法吗? ?还有其他一些功能吗?

Is there any replacement for findfirst() and findnext().
I am using microsoft visual c++ 2010 express and it doesn't support these functions neither the header file <dir.h> ?

I was looking to count the number of files in the directory using these functions but i am having a problem without these functions.

If there is no replacement of the mentioned functions is there any other way out. ? Some other functions ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

手心的海 2024-11-25 11:16:36

正如“iammilind”在评论中所说(可能值得回答) - 您可以使用 Windows api 的 FindFirstFileFindNextFile 函数,您只需填写一个结构体并迭代后者,直到到达无效句柄。这些函数确实可以在控制台上运行,但您必须包含“Windows.h”标头。

然而,这些函数确实存在一些缺陷,如果您希望代码在 Windows 以外的任何设备上运行,您最好使用另一个标头/库(例如 vBx 提到的 Boost::Filesystem)。

另外,这可能会有所帮助:
C++ - 加载全部文件名+统计当前目录下的文件数+过滤文件扩展名

As 'iammilind' said in the comments (probably worthy of an answer) - you can use the windows api's FindFirstFile and FindNextFile functions, you just have to fill up a struct and iterate through the latter until you reach an invalid handle. These functions do work on console, but you must include the 'Windows.h' header.

These functions do come with a couple of pitfalls however, and if you want your code to run on anything other than windows you're probably better off using another header/library (such as Boost::Filesystem, mentioned by vBx).

Also, this may be of help:
C++ - Load all filename + count the number of files in a current directory + filter file extension

随风而去 2024-11-25 11:16:36

您可以使用 Boost.Filesystem

You can use Boost.Filesystem for that

长伴 2024-11-25 11:16:36

在 Windows 中,您可以使用:_findnext、_findnext64、_findnexti64、_wfindnext、_wfindnext64、_wfindnexti64

In Windows you can use: _findnext, _findnext64, _findnexti64, _wfindnext, _wfindnext64, _wfindnexti64

汹涌人海 2024-11-25 11:16:36

如果您使用 MinGW Developer Studio,这可能会有所帮助:

假设您要查看的目录中有文件:

sample1.txt
sample2.txt
sample3.txt

与模式“s*”匹配的两个文件的代码将是:

#include<stdio.h>
#include<io.h>

int main()
{
    // the input pattern and output struct      
    char *pattern = "s*";
    struct _finddata_t fileinfo;

    // first file (sample1.txt)
    int x = _findfirst(pattern, &fileinfo);
    printf("%s" ,fileinfo.name);

    // next file (sample2.txt)
    _findnext(x, &fileinfo);
    printf("%s" ,fileinfo.name);

}

If you use MinGW Developer Studio, this might help:

Assuming that you have the files in the dir you want to look will be:

sample1.txt
sample2.txt
sample3.txt

The code for the two files matching the pattern "s*" will be:

#include<stdio.h>
#include<io.h>

int main()
{
    // the input pattern and output struct      
    char *pattern = "s*";
    struct _finddata_t fileinfo;

    // first file (sample1.txt)
    int x = _findfirst(pattern, &fileinfo);
    printf("%s" ,fileinfo.name);

    // next file (sample2.txt)
    _findnext(x, &fileinfo);
    printf("%s" ,fileinfo.name);

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文