The first way to read all the files is to use an iterative approach, first use the return value of function A to determine if there are files in the directory, and then return the value is valid in the loop with function B until the return value of function B is not valid. Finally, the resource is freed with function C.
1. Open Directory
#include <sys/types.h> #include <dirent.h>dir *opendir (const char *name);
Looking at Linux first, the return is dir*, so the error returns Null (0). And there is no need to care about the definition of the DIR structure, just know it to operate (note: dir is not the structure to save the file information)
There are many methods of windows, including MFC's CFileFind class, WINAPI's Win32_find_data, and the C run-Library _finddata_t, where the last
intptr_t _findfirst ( const char *filespec, struct _finddata_t *fileinfo );
The return type is intptr_t, which is used to represent the type of distance between two pointers (addresses) (for example, pointer-type P1,P2, intptr_t to receive P1-P2 return values), such as a pointer on a 32-bit system that is 4 bytes, and 8 bytes on a 64-bit system through # IFDEF macros to implement cross-platform types.
The return value here is actually a handle that identifies all the files in the current directory rather than the actual pointer type, so it returns 1 instead of 0 (NULL) when an error occurs.
Looking at the input parameters, the 1th parameter is filespec (not filename), which is easy to use because it is not as simple as the Linux opendir to receive the directory name, but rather to receive a specific format. For example, C:\*.* is a search for all types of files under C, and C:\*.txt is a search for all txt files under C. The 2nd parameter is the struct _finddata_t is the structure that actually stores the file information.
2. Traversing files
Each file has a specific structure to describe its properties, here only the file name as an example, the other properties are not specific, the definition can find the document.
#include <dirent.h>struct dirent *readdir (DIR *dirp);
Linux method is still very simple, the first step to get the dir* as input parameters, the method succeeds in returning to the current file Dirent*,struct dirent that is the structure of saving the file information
struct Dirent { ino_t D_ino; /* Inode number */ off_t D_off; /* not an offset; See below */ unsigned short d_reclen; /* Length of this record */ unsigned char d_type; /* Type of file; Not supported by all filesystem types */ char d_name[256];/* null-terminated filename */ };
Return NULL pointer if read fails NULL (0)
Look at the methods under Windows
int _findnext ( intptr_t handle, struct _finddata_t *fileinfo );
The return value here is int, which still returns-1 on error. (c there is no exception handling mechanism, but the use of naïve error code mechanism, so the birth of a beginner is very confused about the question: return 0 is the right or wrong to represent it? For error codes, 0 is often correct, while 0 for pointers means failure, which is null pointer
The 1th input parameter is also the return value of the 1th function, and the 2nd input parameter is also a pointer to the struct body of the description file. struct _finddata_t, which is the structure of saving file information, is also a cross-(Windows) platform definition, with 32-bit systems as an example
struct _finddata32_t{ unsigned attrib; __time32_t time_create; -1 for FAT file systems __time32_t time_access; -1 for FAT file systems __time32_t time_write; _fsize_t size; Char name[260];};
3. Close the Directory
#include <sys/types.h> #include <dirent.h>int closedir (DIR *dirp);
Under Linux, the input parameter is the return value of the 1th function, where the return value is no longer a pointer, but an error code, so the return value of 0 o'clock means that the directory stream is closed successfully, and 1 represents a failure
int _findclose ( intptr_t handle );
Under Windows, the input parameter is the return value of the 1th function, which returns 0 for closing handle success, and 1 for failure.
Finally, we give the sample code for all the files that traverse directories on Linux and Windows (in order to simplify ignoring error handling)
#include <stdio.h> #include <dirent.h>int main (int argc, char** argv) { struct dirent *direntp; DIR *dirp = Opendir ("/"); if (dirp! = null) { while ((DIRENTP = Readdir (dirp)) = NULL) printf ("%s\n", direntp->d_name); } Closedir (DIRP); return 0;}
Windows (c + +) #include <stdio.h> #include <stdlib.h> #include <io.h> //Windows CRT Library # include <string>int Main () {_finddata_t fd;intptr_t handle;std::string dir_name = "c:\\"; if (handle = _findfirst ((dir_ Name + "* *"). C_STR (), &FD))! =-1) {while (_findnext (handle, &FD)! =-1) printf ("%s\n", Fd.name);} _findclose (handle); return 0;}
Since Windows also attaches a string of strings to the Dir_name, it is std::string with a char array and then strcpy too cumbersome.
In contrast, Windows is a pointer to the structure of the file as input parameters, and Linux as a return parameter, Linux is a more natural approach, and even in the C-style, the code is very easy to understand.
However, it is important to note that Linux is actually dynamically requesting space and requires manual free (DIRENTP) to release memory, although apue The example code above does not have this step.
Comparison of methods for all files in the Traverse directory of Linux and Windows