C Learning Record 1

Source: Internet
Author: User

1. ftruncate: Change the file size

Header file:# Include <unistd. h>

Function Definition:Intftruncate (int fd, off_t length );

Function Description:Ftruncate () changes the file size specified by the parameter FD to the size specified by the parameter length. The FD parameter is an opened file description and must be a file opened in write mode. If the size of the original file is greater than the parameter length, the excess part will be deleted.

Return Value:If the execution succeeds, 0 is returned. If the execution fails,-1 is returned. The error cause is errno.

Error:

The description of the FD file is invalid or the file is closed.

The FD Val parameter FD is a socket, not a file, or the file is not opened in write mode.

2.Access: Determine whether you have the permission to access files.
Header file:
Unistd. h
Function Definition:Int access (const char * pathname, int mode );
Function Description:Access () checks whether an existing file can be read/written. The mode parameter can be combined in several cases. r_ OK, w_ OK, x_ OK, and f_ OK. r_ OK, w_ OK, and x_ OK are used to check whether the file has read, write, and execute permissions. F_ OK is used to determine whether the file exists. Because access () is only used for permission verification and does not care about the file format or content, if a directory is represented as "writable ", indicates that new files can be created in the directory, rather than being processed as files. If all the permissions to be checked have passed the check, a value of 0 is returned, indicating success. If a permission is disabled,-1 is returned.

Error code:
The file specified by the eaccess parameter pathname does not meet the required permissions.
The file to be tested with write permission by erofs exists in the read-only file system.
The pathname pointer of the efault parameter exceeds the accessible memory space.
The Mode Val parameter mode is incorrect.
The pathname parameter of enametoolong is too long.
The pathname parameter of enotdir is a directory.
Insufficient enomem core memory
The pathname of the eloop parameter has too many symbolic connections.
Eio I/O Access Error

3.Fstat:Get the File status by the file description

Header file: # Include <sys/STAT. h>, # include <unistd. h>

Function Definition:Int fstat (INT Fildes, struct stat * BUF );

Function Description:Fstat () is used to copy the file status referred to by the Fildes parameter to the structure referred to by the Buf parameter (struct Stat ). fstat () and Stat () Act exactly the same. The difference is that the input parameter is an opened file description word. For more information, see Stat ().

Return Value: if the execution is successful, 0 is returned. If the execution fails,-1 is returned.

Error:The error code is stored in errno.

 

4. Stat, lstat, fstat1:Get file attributes

Header file:# Include <sys/STAT. h>

Function Definition:

Int Stat (const char * restrict pathname, struct stat * restrict BUF); provides the file name and obtains the corresponding attributes of the file.

Int fstat (INT filedes, struct stat * BUF); get the attributes of the file through the file descriptor.

Int lstat (const char * restrict pathname, struct stat * restrict BUF); connection file description to get file attributes.

 

Function Description:The stat, lstat, and fstat1 functions are used to obtain attributes of files (common files, directories, pipelines, sockets, characters, blocks. All three system calls can return the status information of the specified file, which is written to the buffer zone of the struct stat structure. By analyzing this structure, you can obtain the information of the specified file.

File attributes:

Struct stat

{

Mode_t st_mode; // file mode, file, directory, etc.

Ino_t st_ino; // inode node number

Dev_t st_dev; // device number

Dev_t st_rdev; // special device number

Nlink_t st_nlink; // number of file connections

Uid_t st_uid; // file owner

Gid_t st_gid; // group corresponding to the file owner

Off_t st_size; // common file, corresponding to the number of file bytes, that is, the file size

Time_t st_atime; // time when the object was last accessed

Time_t st_mtime; // The last modification time of the file content

Time_t st_ctime; // File status change time

Blksize_t st_blksize; // block size corresponding to the file content

Blkcnt_t st_blocks; // Number of chunks corresponding to Wei Jian content

};

5.FTW: traverse the directory tree
Header file:
# Include <FTW. h>
Function Definition:Int FTW (const char * Dir, INT (* fN) (const char * file, const struct stat * Sb, int flag), int depth)
Function Description:FTW () recursively traverses sub-directories from the directory specified by the Dir parameter. Each time you enter a directory, the * fN defined function is called for processing.

Parameter description:FTW () will pass three parameters to FN ():

First Parameter* File points to the current directory path

Second ParameterYes * Sb, which is the stat structure pointer (for the definition of this structure, see Stat ()),

Third ParameterAs the flag, there are several possible values:
Ftw_f common file
Ftw_d directory
A directory that cannot be read by ftw_dnr.
Ftw_sl symbolic connection
Ftw_ns cannot obtain the stat structure data, which may be caused by permission issues.
Last ParameterDepth indicates the number of files that can be opened simultaneously by FTW () during Directory Traversal

FTW () requires at least one file descriptive word in each layer of the time duration. If the time duration is used up, the whole traversal will be slow because the file is constantly closed and the operation to open the file appears. If you want to end the FTW () traversal, FN () only needs to return a non-zero value. This value will also be the return value of FTW (); otherwise, FTW () will try to finish all the directories and then return 0. If the traversal is interrupted, the return value of the FN () function is returned. If all traversal is complete, 0 is returned. -1 is returned if an error occurs.
Additional instructions:Since FTW () dynamically configures memory usage, use the normal method (non-0 value returned by FN function) to interrupt traversal. Do not use longjmp () in FN function ().

6.Get_current_dir_name: Get the current working directory
Header file:
# Include <unistd. h>
Function Definition:Char * get_current_dir_name (void );
Function Description:This function returns a string pointer pointing to the absolute path string of the current working directory.

Return Value:If the execution succeeds, a string pointer is returned. If the execution fails, null is returned. The error code is stored in errno.

7.Getcwd: Get the current working directory
Header file:
# Include <unistd. h>
Function Definition:Char * getcwd (char * Buf, size_t size );
Function Description:Getcwd () copies the absolute path of the current working directory to the memory space specified by the Buf parameter. The parameter size is the Buf space. When calling this function, the Buf refers to a large enough memory space. If the length of the absolute path string in the working directory exceeds the size of the parameter, the return value is null, And the errno value is erange. If the Buf parameter is null, getcwd () will automatically configure the memory according to the size of the parameter size (using malloc (). If the parameter size is also 0, getcwd () the memory size is determined based on the degree of the absolute path of the working directory. After using this string, the process can use free () to release the space.

Return Value:If the execution succeeds, the result is copied to the memory space specified by the Buf parameter, or the automatically configured string pointer is returned. If a failure occurs, null is returned. The error code is stored in errno.
8. getwd: Get the current working directory
Header file:
# Include <unistd. h>
Function Definition:Char * getwd (char * BUF );
Function Description:Getwd () copies the absolute path of the current working directory to the memory space specified by the Buf parameter, and returns the string pointer of this path.

Return Value:If the execution succeeds, the result is copied to the memory space specified by the Buf parameter. If the execution fails, null is returned, and the error code is stored in errno.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.