c - 你好。我正在尝试用 C 语言编写文件索引程序

标签 c file indexing messagebox

我正在制作一个文件索引程序。 我从谷歌找到了一个来源。

原来是这样的...

==================

void main()
{
    _finddata_t fd;
    long handle;
    int result = 1;
    handle = _findfirst(".\\*.*", &fd); 

    if (handle == -1)
    {
        printf("There were no files.\n");
        return;
    }

    while (result != -1)
    {
        printf("File: %s\n", fd.name);
        result = _findnext(handle, &fd);
    }

    _findclose(handle);

    return;
}

====================

它有效。但我想获取找到的文件的数量并将其显示到消息框。

所以我尝试使用这段代码...

============

void main()
{
    _finddata_t fd;
    long handle;
    int result = 1;
    handle = _findfirst(".\\*.*", &fd);  //현재 폴더 내 모든 파일을 찾는다.
    int i = 0;
    LPWSTR str = NULL;

    if (handle == -1)
    {
        printf("There were no files.\n");
        return;
    }

    while (result != -1)
    {
        printf("File: %s\n", fd.name);
        result = _findnext(handle, &fd);
        i++;
    }

    _findclose(handle);

    wsprintf(str, L"%d Files were found", i);
    MessageBox(NULL, str, L"Result", MB_OK);
    return;
}

============

这不起作用。它有这个错误...

Exception thrown at 0x76C73566 (user32.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation writing location 0x00000000.

我该如何解决它并实现我的目的? 请帮助我。

最佳答案

您声明并初始化 str 为 NULL。

LPWSTR str = NULL;

那么你就不能这样做 wsprintf(str, L"%d Files were found", i);

关于c - 你好。我正在尝试用 C 语言编写文件索引程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32943614/

相关文章:

c - 自动局部变量是否存储在C中的堆栈中?

c++ - 如何在winapi C/C++中使用系统mui资源?

c - 如何从 C 语言的文件中逐行读取每行两个字符串

java - 尝试在 Java 中构建

postgresql - 每个表的索引数

mysql - 什么是空间索引,我应该什么时候使用它?

c - (C 套接字编程)来自服务器的单独 send() 调用在同一个客户端 recv() 缓冲区中结束

c - 服务器中的 Leader/Follower pthread 实现

java - 在 Java 中获取所有 DVD 驱动器

Python 索引冲突