File I/O functions (open,read,write,lseek,close)

Source: Internet
Author: User

Most Unix file I/O requires only 5 functions: Open,read,write,lseek,close. These functions are non-cached I/O, without caching refers to a system call in the kernel that is called by each read and write. These functions use three header files: sys/types.h,sys/stat.h,fcntl.h
#include <fcntl.h>
#include <types.h>
#include <sys/stat.h>/* This header file defines the mode flag */
Open function: int open (const char path *name, int oflag,...);
Returns: If the file descriptor is open for write-only, if the error is-1.
Call the Open function to open or create a file that uses the third parameter only when creating a new file, and the file descriptor returned by the Open function must be the smallest unused descriptor number.
The first parameter is the path name
The second parameter is a file status flag:
O_rdonly or 0: readable
O_wronly or 1: writable
O_rdwr or 2: Executable
O_append: Add to the end of the file each time you write
O_creat: If this file does not exist, create it, when using this option, it is necessary to describe the third parameter mode, which describes the access permission of the new file.
O_EXCL: If O_creat is specified at the same time, and the file already exists, an error occurs, which tests whether a file exists and if it does not, creating the file becomes an atomic operation
O_trunc: If this file exists and is open for read-only or write-only success, truncate its length to 0
O_noctty: If pathname refers to an end device, it is not assigned as the control terminal for this process
O_nonblock: If pathname refers to a FIFO, a block special file, or a character special file, this option sets the non-blocking mode for this open operation and subsequent I/O operations for this file
O_sync: Make each write wait until the physical I/O operation is complete
The third parameter is typically file permissions:
s_irwxu,0700: Indicates that the file owner has readable, writable, and can point to permissions
S_IRUSR or s_iread,0400: Has readable permissions on behalf of the file owner
S_IWUSR or s_iwrite,0200: Has writable permissions on behalf of the file owner
S_IXUSR or s_iexec,0100: Indicates that the file owner has permission to point to
s_irwxg,0070: Represents the file user group with readable, writable, executable permissions
s_irgrp,0040: A user group with readable permissions on behalf of the file
s_iwgrp,0020: A writable permission on behalf of the file user group
S_IXGRP,0010: Represents the file user group with executable permissions
S_irwxo,0007: On behalf of other users with readable, writable, enforceable permissions
S_IROTH,0004: Has readable permissions on behalf of other users
S_IWOTH,0002: Writable permissions on behalf of other users
S_IXOTH,0001: Has executable permissions on behalf of other users
creat function: int creat (const char *pathname, mode_t mode);
Returns: If the file descriptor is open only for write-only, if the error is-1
This function is equivalent to: open (pathname,o_wronly| O_creat| O_trunc,mode);
Used to create a new file
Note:The third parameter is used when there is o_creat in the second argument. If not, the third argument can be ignored。

the difference between the open function and the fopen function is that system calls such as creat open,close,read,write,lseek use the underlying file descriptor to identify the file, and the file descriptor is specific to the unix/linux and is not portable. Fopen,fclose,fread,fseek is a function in the C-language I/O standard library. When writing cross-platform programs, it is easier to migrate with standard library functions.

Read function: ssize_t read (int filedes, void *buff, size_t nbytes);
Returns: the number of bytes read, if the end of the file is 0, if the error is-1
There are a number of situations in which the number of bytes actually read is less than the number of bytes required to read:
(1) When reading a normal file, it has reached the end of the file before reading the required number of bytes, for example, if there are 30 bytes before reaching the end of the file, and requires reading 100 bytes, then read returns 30, and the next time, when read is called, it will return 0.
(2) When reading from an end device, it is usually read one line at a time
(3) When reading from the network, the buffer mechanism in the network may cause the return value to be less than the number of bytes required to read.
(4) Some record-oriented devices, such as tapes, return up to one record at a time
The read operation starts at the current offset of the file and increases the number of bytes actually read before returning successfully.
Write function: ssize_t write (int filedes, const void *buff,size_t nbytes);
Returns: If the number of bytes written is successful, if the error is-1
Close function: int close (int filedes);
Return: If the success is 0, if the error is-1
Closing a file also frees all record locks that the process adds to the file, and when a process terminates, all of its open files are automatically closed by the kernel.
Lseek function: off_t lseek (int filedes, off_t offset, int whence);
Return: If the new file is successfully displaced, if the error is-1
Each open file has a "current file offset" associated with it, which is a nonnegative integer that measures the number of bytes computed at the beginning of the file. By default, when you open a file, the offset is set to 0 unless you specify the O_append option. Normally read, the write operation starts at the current file offset and increases the amount of bytes read or written by the offset. You can call Lseek to explicitly locate an open file.
The interpretation of the parameter offset is related to the value of the parameter whence.
If whence is seek_set, set the offset of the file to offset bytes from the beginning of the file
If whence is seek_cur, the offset of the file is set to its current value plus offset,offset can be positive or negative
If whence is seek_end, set the file's offset to the file length plus offset,offset can be positive or negative
Early use of 0,1,2 iron seek_set,seek_cur,seek_end.
Some devices may also allow negative displacements, but for normal files, their displacement must be non-negative
Example:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main (void)
{
int fd, size;
Char buf1[]= "Hello, World";
Char buf2[50];
if ((Fd=open ("/home/sam/helloworld", o_creat| o_trunc| o_rdwr,0666)) ==-1)
{
printf ("Open or create file named \" helloworld\ "failed.\n");
Exit (1);
}
Write (fd,buf1,sizeof (BUF1));
Close (FD);
if ((Fd=open ("/home/sam/helloworld", o_rdonly) ==-1)
{
printf ("Open file named \" helloworld\ "failed.\n");
Exit (1);
}
Size=read (fd,buf2,sizeof (BUF2));
Close (FD);
printf ("%s\n", buf2);
if ((Fd=open ("/home/sam/helloworld", o_rdonly) ==-1)
{
printf ("Open file named \" helloworld\ "failed.\n");
Exit (1);
}
Lseek (Fd,6,seek_set);
Size=read (fd,buf2,sizeof (BUF2));
printf ("%s\n", buf2);
Close (FD);
return 0;
}

File I/O functions (open,read,write,lseek,close)

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.