代码:
#include <Windows.h>
#include <atlstr.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
CString dir;
dir="f:\\*.txt";
LPCTSTR bufDir;
bufDir=(LPCTSTR)dir;
wcout << "Target file is " << bufDir << endl;
hFind = FindFirstFile(bufDir, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
wcout << "Invalid File Handle. GetLastError reports " << GetLastError () << endl;
return (0);
}
else
{
wcout << "The first file found is " << FindFileData.cFileName << endl;
FindClose(hFind);
system("pause");
return (1);
}
}
Contains information about the file that is found by the FindFirstFile, FindFirstFileEx, orFindNextFile function.
包括由FindFirstFile, FindFirstFileEx, or FindNextFile 返回的信息
typedef struct _WIN32_FIND_DATA {
DWORD dwFileAttributes;
FILETIME ftCreationTime;
FILETIME ftLastAccessTime;
FILETIME ftLastWriteTime;
DWORD nFileSizeHigh;
DWORD nFileSizeLow;
DWORD dwReserved0;
DWORD dwReserved1;
TCHAR cFileName[MAX_PATH];
TCHAR cAlternateFileName[14];} WIN32_FIND_DATA,
*PWIN32_FIND_DATA,
*LPWIN32_FIND_DATA;
Members
FILE_ATTRIBUTE_ARCHIVE 标记归档属性
FILE_ATTRIBUTE_COMPRESSED 将文件标记为已压缩,或者标记为文件在目录中的默认压缩方式
FILE_ATTRIBUTE_NORMAL 默认属性
FILE_ATTRIBUTE_HIDDEN 隐藏文件或目录
FILE_ATTRIBUTE_READONLY 文件为只读
FILE_ATTRIBUTE_SYSTEM 文件为系统文件
FILE_FLAG_WRITE_THROUGH 操作系统不得推迟对文件的写操作
FILE_FLAG_OVERLAPPED 允许对文件进行重叠操作
FILE_FLAG_NO_BUFFERING 禁止对文件进行缓冲处理。文件只能写入磁盘卷的扇区块
FILE_FLAG_RANDOM_ACCESS 针对随机访问对文件缓冲进行优化
FILE_FLAG_SEQUENTIAL_SCAN 针对连续访问对文件缓冲进行优化
FILE_FLAG_DELETE_ON_CLOSE 关闭了上一次打开的句柄后,将文件删除。特别适合临时文件
cFileName The name of the file