使用 C++ 的迭代目录列表出错
Error in iterative directory listing using C++
我遇到了使用C++在Windows上列出目录的代码,并且我知道您可以使用以下API搜索目录:FindFirstFileEx,FindNextFile和CloseFind。您需要 #include,这将使您能够访问Windows API。
我无法理解这段代码是如何工作的,因此无法找出编译错误。
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strcat.h>
#pragma comment(lib, "User32.lib")
void DisplayErrorBox(LPTSTR lpszFunction);
int _tmain(int argc, TCHAR *argv[])
{
WIN32_FIND_DATA ffd;
LARGE_INTEGER filesize;
TCHAR szDir[MAX_PATH];
size_t length_of_arg;
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError=0;
// If the directory is not specified as a command-line argument,
// print usage.
if(argc != 2)
{
_tprintf(TEXT("nUsage: %s <directory name>n"), argv[0]);
return (-1);
}
// Check that the input path plus 3 is not longer than MAX_PATH.
// Three characters are for the "*" plus NULL appended below.
StringCchLength(argv[1], MAX_PATH, &length_of_arg);
if (length_of_arg > (MAX_PATH - 3))
{
_tprintf(TEXT("nDirectory path is too long.n"));
return (-1);
}
_tprintf(TEXT("nTarget directory is %snn"), argv[1]);
// Prepare string for use with FindFile functions. First, copy the
// string to a buffer, then append '*' to the directory name.
StringCchCopy(szDir, MAX_PATH, argv[1]);
StringCchCat(szDir, MAX_PATH, TEXT("\*"));
// Find the first file in the directory.
hFind = FindFirstFile(szDir, &ffd);
if (INVALID_HANDLE_VALUE == hFind)
{
DisplayErrorBox(TEXT("FindFirstFile"));
return dwError;
}
// List all the files in the directory with some info about them.
do
{
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
_tprintf(TEXT(" %s <DIR>n"), ffd.cFileName);
}
else
{
filesize.LowPart = ffd.nFileSizeLow;
filesize.HighPart = ffd.nFileSizeHigh;
_tprintf(TEXT(" %s %ld bytesn"), ffd.cFileName, filesize.QuadPart);
}
}
while (FindNextFile(hFind, &ffd) != 0);
dwError = GetLastError();
if (dwError != ERROR_NO_MORE_FILES)
{
DisplayErrorBox(TEXT("FindFirstFile"));
}
FindClose(hFind);
return dwError;
}
void DisplayErrorBox(LPTSTR lpszFunction)
{
// Retrieve the system error message for the last-error code
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
// Display the error message and clean up
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}
在代码块上给出以下错误:
fatal error: strcat.h: No such file or directory
#include <strcat.h>
^
编译终止。
strcat
是在string.h
而不是strcat.h
中定义的
然后,您似乎没有使用strcat
,也许您可以将其删除。
相关文章:
- 读取文件的最后一行并输入到链接列表时出错
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 基于数组的列表 - 动态数组创建时出错
- 编译包含指向模板函数的指针的初始值设定项列表时,gcc 出错,但 clang 不出错
- 方法的参数列表出错
- 使用 C++ 的迭代目录列表出错
- 尝试访问列表时出错
- 使用列表 STL 时出错
- C2057构造函数初始化列表中的常量值定义出错
- 使用变量参数列表为传递的引用赋值(VS2010中出错)
- 创建模板树节点列表时出错
- 通过函数后从列表中删除项目时出错
- 链接列表的Push_back函数出错
- 泛型列表节点类中出错
- C++实现邻接列表时出错
- 运行列表的实现时出错
- 使用链接列表"Stop working"的 c++ 代码中出错
- g++4.8.2上列表方法参数默认初始化时出错
- 将大括号括起来的初始值设定项列表转换为类型时出错的含义
- 指针列表出错