文件查找01

一.文件查找的关键函数

01. FindFirstFile 

     		HANDLE WINAPI FindFirstFile(
 						 _In_  LPCTSTR           lpFileName, 
						_Out_ LPWIN32_FIND_DATA lpFindFileData
						);
入参:
lpFileName:
	【In】Pointer to a null-terminated string that specifies a valid directory or path and file name
	出参:
lpFindFileData:
 	【Out】Pointer to the WIN32_FIND_DATA structure that receives information about the found file or subdirectory  
		
	摘自MSDN:			

WIN32_FIND_DATA

The WIN32_FIND_DATA structure describes a file found by the FindFirstFile, FindFirstFileEx, or FindNextFile function.

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; 
Members
dwFileAttributes
Specifies the file attributes of the file found. This member can be one or more of the following values.
AttributeMeaning
FILE_ATTRIBUTE_ARCHIVEThe file or directory is an archive file or directory. Applications use this attribute to mark files for backup or removal.
FILE_ATTRIBUTE_COMPRESSEDThe file or directory is compressed. For a file, this means that all of the data in the file is compressed. For a directory, this means that compression is the default for newly created files and subdirectories.
FILE_ATTRIBUTE_DIRECTORYThe handle identifies a directory.
FILE_ATTRIBUTE_ENCRYPTEDThe file or directory is encrypted. For a file, this means that all data in the file is encrypted. For a directory, this means that encryption is the default for newly created files and subdirectories.
FILE_ATTRIBUTE_HIDDENThe file or directory is hidden. It is not included in an ordinary directory listing.
FILE_ATTRIBUTE_NORMALThe file or directory has no other attributes set. This attribute is valid only if used alone.
FILE_ATTRIBUTE_OFFLINEThe file data is not immediately available. This attribute indicates that the file data has been physically moved to offline storage. This attribute is used by Remote Storage, the hierarchical storage management software in Windows 2000. Applications should not arbitrarily change this attribute.
FILE_ATTRIBUTE_READONLYThe file or directory is read-only. Applications can read the file but cannot write to it or delete it. In the case of a directory, applications cannot delete it.
FILE_ATTRIBUTE_REPARSE_POINTThe file has an associated reparse point.
FILE_ATTRIBUTE_SPARSE_FILEThe file is a sparse file.
FILE_ATTRIBUTE_SYSTEMThe file or directory is part of the operating system or is used exclusively by the operating system.
FILE_ATTRIBUTE_TEMPORARYThe file is being used for temporary storage. File systems attempt to keep all of the data in memory for quicker access, rather than flushing it back to mass storage. A temporary file should be deleted by the application as soon as it is no longer needed.

ftCreationTime
Specifies a FILETIME structure containing the time the file was created. FindFirstFile and FindNextFile report file times in Coordinated Universal Time (UTC) format. These functions set the FILETIME members to zero if the file system containing the file does not support this time member. You can use the FileTimeToLocalFileTime function to convert from UTC to local time, and then use the FileTimeToSystemTime function to convert the local time to a SYSTEMTIME structure containing individual members for the month, day, year, weekday, hour, minute, second, and millisecond.
ftLastAccessTime
Specifies a FILETIME structure containing the time that the file was last accessed. The time is in UTC format; the FILETIME members are zero if the file system does not support this time member.
ftLastWriteTime
Specifies a FILETIME structure containing the time that the file was last written to. The time is in UTC format; the FILETIME members are zero if the file system does not support this time member.
nFileSizeHigh
Specifies the high-order DWORD value of the file size, in bytes. This value is zero unless the file size is greater than MAXDWORD. The size of the file is equal to ( nFileSizeHigh * ( MAXDWORD+1)) + nFileSizeLow.
nFileSizeLow
Specifies the low-order DWORD value of the file size, in bytes.
dwReserved0
If the dwFileAttributes member includes the FILE_ATTRIBUTE_REPARSE_POINT attribute, this member specifies the reparse tag. Otherwise, this value is undefined and should not be used.
dwReserved1
Reserved for future use.
cFileName
A null-terminated string that is the name of the file.
cAlternateFileName
A null-terminated string that is an alternative name for the file. This name is in the classic 8.3 (filename.ext) file name format
返回值:HANDLE
	【Return】:

If the function succeeds, the return value is a search handle used in a subsequent call to FindNextFile or FindClose.

If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError

02.FindNextFile
		BOOL FindNextFile(
 					 HANDLE hFindFile,                // search handle 
					  LPWIN32_FIND_DATA lpFindFileData // data buffer
				);
		
·····Parameters:
···hFindFile
·········[in] Search handle returned by a previous call to the FindFirstFile function.
···lpFindFileData ··········[out] Pointer to the WIN32_FIND_DATA structure that receives information about the found file or subdirectory. The structure can be used in subsequent calls to FindNextFile to refer to the found file or directory.
·····Return Values

··If the function succeeds, the return value is nonzero.

··If the function fails, the return value is zero. To get extended error information, call GetLastError. If no matching files can be found, the GetLastError function returns ERROR_NO_MORE_FILES.

、C++中的原子操作

 

········表示在多个线程访问同一个全局资源的时候,能够确保所有其他的线程都不在同一时间内访问相同的资源。也就是他确保了在同一时刻只有唯一的线程对这个资源进行访问。这有点类似互斥对象对共享资源的访问的保护,但是原子操作更加接近底层,因而效率更高。

········新标准C++11,引入了原子操作的概念,并通过这个新的头文件提供了多种原子操作数据类型,例如,atomic_bool,atomic_int等等,如果我们在多个线程中对这些类型的共享资源进行操作,编译器将保证这些操作都是原子性的,也就是说,确保任意时刻只有一个线程对这个资源进行访问,编译器将保证,多个线程访问这个共享资源的正确性。从而避免了锁的使用,提高了效率

、*.*--所有文件

···在windows中,*代表了所有字符,而*.*的意思是windows下的所有文件名和文件格式,比如在C:\123下有几个文件,123.exe 234.jif 345.ppt 564.bat这些文件的文件名和后缀名不同,当我们不知道怎么概括C:\123下面的文件的时候,可以说成C盘123文件夹的*.*”

四、多线程下记数

原子操作也无法确保记数的准确性,因为线程是抢占性的,不是按照顺序进行的。

如:在线程开始时,线程总数+1;线程退出的时候,线程总数-1;线程数为0时,结束进程;

线程的抢占性,不能保证+1操作一定在-1操作之前,可能会造成线程总数为0时,还有线程没有退出,造成错误。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值