C local file operations

Source: Internet
Author: User

1. Linux platform C implements local file CP

Code sub-and Running Effect Test

Compile code child;

Run the executable program;

2. c. Copy files through pipelines

3. Next episode-File Read/Write File Location operations;

Linux lseek function usage;
Lseek ()
Header file # include <sys/types. h>
# Include <unistd. h>
Prototype:
Off_t lseek (INT Fildes, off_t offset, int whence); // offset, Root

Function Description: each opened file has a read/write location. When a file is opened, its read/write Location usually points to the beginning of the file;
If you open a file (o_append) as an attachment, the read/write position points to the end of the file;
When read () or write (), the read/write location increases accordingly.
Lseek () is used to control the read/write location of a file.
The Fildes parameter is an opened file descriptor, And the offset parameter is the number of read/write locations to be moved Based on the whence parameter;
The whence parameter is one of the following
The seek_set parameter offset is the new read/write location.
Seek_cur: Increase the offset displacement after the current read/write position
Seek_end points the read/write position to the end of the file and then increases the offset displacement.

When the whence value is seek_cur or seek_end, the offset parameter allows negative values.
Special usage
1) to move the read/write location to the beginning of the file: lseek (INT Fildes, 0, seek_set );
2) Move the read/write location to the end of the file: lssek (INT Fildes, 0, seek_end)
3) obtain the current file location: lseek (INT Fildes, 0, seek_cur)

Return Value: when the call is successful, the current read/write location is returned, that is, the number of characters before the start of the file,
If an error occurs,-1 is returned, and errno stores the error number;
Error code: eintr system interruption;

C local file operations

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.