你好。我试图用C获取文件索引程序

时间:2015-10-05 07:48:54

标签: 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;
}

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

有效。但我想获取已找到文件的数量并将其显示到MessageBox。

所以我尝试了这段代码......

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

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.

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

2 个答案:

答案 0 :(得分:0)

您声明并初始化str为NULL。

LPWSTR str = NULL;

然后你无法做wsprintf(str, L"%d Files were found", i);

答案 1 :(得分:0)

LPWSTR str = NULL; 然后 wsprintf(str, L"%d Files were found", i);

您期望发生什么?你需要分配一些空间。