簡體   English   中英

你好。 我正在嘗試使用C獲取文件索引程序

[英]Hello. I'm trying to get file indexing program with C

我正在制作文件索引程序。 我從Google找到了一個來源。

原來是這樣的...

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

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.

如何解決並實現目標? 請幫我。

您聲明並初始化str為NULL。

LPWSTR str = NULL;

然后,您將無法執行wsprintf(str, L"%d Files were found", i);

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

您期望發生什么? 您需要分配一些空間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM