C language Get current work path (turn)
-------------------------------------------------------------------------------------
Linux
-------------------------------------------------------------------------------------
Function Name: GETCWD
Function: Get the current working directory
Usage: char *getcwd (char *buf, size_t size);
Function Description: GETCWD () copies the current working directory absolute path to the memory space that the parameter buf refers to, and the parameter size is the space size of the BUF. When this function is called, the memory space referred to by BUF is large enough, and if the length of the absolute path of the working directory exceeds the parameter size, the Null,errno value is erange. If the parameter buf is NULL,GETCWD () automatically configures the memory according to the size of the parameter (using malloc ()) and if the parameter size is 0, GETCWD () determines the configured memory size according to the string extent of the absolute path to the working directory. The process can use free () to release this space after the string has been used.
Return value: Successful execution copies the result to the memory space referred to in the parameter buf or returns an automatically configured string pointer. The failure returns NULL, and the error code is stored in errno.
program Example:
#include <stdio.h>
#include <unistd.h>
main ()
{
char buf[80];
GETCWD (buf,sizeof (BUF));
printf ("Current working directory:%s\n", buf);
}
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
directory operations under Windows
-------------------------------------------------------------------------------------
1. Get Current working directory
char* _getcwd (char *buffer, int maxlen); Work Energy: Get the current working directory.
Header files: #include <direct.h>
//return value: Successful return of pointer
//failure to the buffer return null, and set errno to one of the following three values:
// Enodev No this device
// Enomem memory
// Erange result out of range//
Note : When the first argument is NULL, the second parameter maxlen length setting is invalid , and the function
/// use malloc allocates enough memory, you need to pass the function return value to the free () function to
// release the memory. When the first argument is not NULL, the maxlen specifies that the length is not sufficient for the function return
// wrong, set errno to Erange
2. Change the current working directory
int _chdir (const char *dirname); Work Energy: Change the current working directory.
Header file: #include <direct.h>//
return value: Successfully returned 0
// failed return-1 and set errno as follows:
// enoent The path does not exist
3. File traversal (Find)
Long _findfirst (char *filespec, struct _finddata_t *fileinfo);
Function: Provides the first file that matches the filespec specified portal. Usually subsequent use of the _findnext//number to complete a generic file traversal.
Header files: #include <io.h>//Parameters: filespec-target file specification, can contain wildcard//fileinfo-File information buffer//return value: Successfully return a unique search handle Error returns-1 and sets errno to the following value://enoent The generic cannot match//einval invalid filename//Note: _finddata_t description struct
_finddata_t {unsigned attrib;
time_t time_create;
time_t time_access;
time_t Time_write;
_fsize_t size;
Char Name[_max_fname];
}; Where://unsigned atrrib: The location where file properties are stored. It stores a unsigned unit that represents the//property of a file. File attributes are represented by a bit, mainly with the following: _a_arch (archive),//_a_hidden (hidden), _a_normal (normal), _a_rdonly (read only),//_ A_subdir (folder), _a_system (System). These are the macros defined in <io.h>/, which can be used directly, and the meaning of itself is actually an unsigned integer//(except that this integer should be a power of 2, thus guaranteeing that only one is 1, and The other//bit is 0). Since it is a bit representation, then when a file has more than one attribute, it is often//through bit or by the way that comesTo the synthesis of several attributes. For example, read-only + hide + System Properties,//should be: _a_hidden |
_a_rdonly |_a_system.
time_t time_create: The time_t here is a variable type that is used to store the file creation time.
time_t time_access: The last time the file was accessed.
time_t Time_write: The time the file was last modified. _fsize_t Size: How large the file is.
The _fsize_t here should be equivalent to the unsigned integer, representing the number of bytes//files. Char Name[_max_fname]: file name of the file.
The _max_fname here is a constant macro that is defined in <stdlib.h> Header/file to represent the maximum length of the file name.
int _findnext (long handle, struct _finddata_t *fileinfo); Function: According to the generic rules in the previous _findfirst, look for the next file that conforms to the generic and use this to modify the value//header file in FileInfo: #include <io.h>//Parameters: Lon G Handle-search handle (usually returned by _findfirst () immediately before it)//fileinfo-File information buffer//return value: Successfully return 0//Error return-1, and set errno as follows
Value://Enoent No more conforming to the generic file int _findclose (long handle);
Function: Close the search handle and release the appropriate resource//header file: #include <io.h>//Parameters: Long handle-search handle (usually returned by _findfirst () immediately before it)//return value: Successfully returned 0 Error returns-1 and sets errno to the following value://Enoent No more matches to the generic file
4. Create a table of contents
int _mkdir (const char *dirname);
Work ENERGY: Create a new directory named DirName.
Header file: #include <direct.h>
//Return value: Successfully returned 0
// failed return-1, and set errno to one of the following three values:
// eaccess permission not allowed
// Eexist The directory already exists
// enoent No such file or directory
5. Delete Directory
int _rmdir (const char *dirname);
Work ENERGY: Deletes the directory named DirName.
Header file: #include <direct.h>
//Return value: Successfully returned 0
// failed return-1, and set errno to one of the following three values:
// eaccess : Permission is not allowed
// Enotempty:dirname is not a folder, or the folder is not empty, or
// dirname is the current working folder; dirname
// for when the root folder; enoent : No such file or directory
6. Other operations
int _access (const char *path, int mode);
Work ENERGY: Determine file/directory access rights.
Header files: #include <io.h>
//Parameters : path-file or directory
/ mode-permission set, its value is as follows:
// existence Only
// Write permission
// Read permission
//Read and Write permission
int _ chdrive (int drive);
Work Energy: Change the current working drive.
Header files: #include <direct.h>
//Return value: Successfully returned 0
// Failure return-1//
Note : Parameter description
// drive = 1: a plate// drive =2: B plate
// drive =3: C disk ...
char* _getdcwd (int drive, char *buffer, int maxlen);
Work Energy: Gets the current working path of the specified drive.
Header files: #include <direct.h>
//return value: Successful return of pointer
//failure to the buffer return null, and set errno to one of the following three values:
// Enodev No this device
// Enomem memory
// Erange result out of range//
Note : When the first argument is NULL, This function sets errno to Erange
Test:
Features: Print all files in directory path that match the pattern Chre: path-directories to print//CHRE-requires matching regular expression static void Printdir (const char* Path, const char* chre) {char* Chcurpath = getcwd (NULL, 0);
Currently working directory printf ("Current work Path:%s\n", Chcurpath);
int ret = _chdir (path);
if (Ret < 0) {perror (path);
} char* NewPath = GETCWD (NULL, 0);
printf ("New Work Path:%s\n", NewPath);
Free (NewPath);
struct _finddata_t data; Long HND = _findfirst (Chre, &data);
Finds matching file names with regular expression chre the first file//returns a unique search handle if (HND < 0) {
Perror (CHRE); int nret = (HND <0)?
-1:1; while (nret >= 0) {if (Data.attrib = = _a_subdir)//If directory printf ("[%s]*\n", Data.name
);
else printf ("[%s]\n", data.name);
Nret = _findnext (HND, &data); }
_FindClose (HND); Closes the current handle chdir (Chcurpath);
Switch back to the previous working directory free (Chcurpath); }
Address: http://blog.csdn.net/yangalbert/article/details/7455241