我正在制作一个文件索引程序。 我从谷歌找到了一个来源。
原来是这样的...
==================
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/