File Operations in linux-general Linux technology-Linux programming and kernel information. For details, refer to the following section. Linux File System
Files in linux are very important, because everything in linux is a file!
This means that in general, programs can treat disk files, serial ports, printers, and other devices as they do with files. Five basic functions are enough to deal with most problems. They are open (open a file or device), close (close a file or device), read (read data from an opened file or device), write (write a file or device), and ioctl (transfer control information to the device driver ).
When a program starts running, there are usually three file descriptors opened for it. They are:
0. Standard Input
1. annotation output
2. Standard errors
1. write System Call
# Include
Size_t write (int filedes, const void * buf, size_t nbytes );
Write the first nbytes bytes in the buffer buf to the file associated with the file descriptor filedes, and return the actual number of written bytes.-1 indicates that an error has occurred, the error code is saved in the global variable errno. Note: It is best not to exceed the buf size of nbytes, otherwise unexpected results may occur.
Example 1: display a piece of information on the standard output
// Write. c
# Include
# Include
Int main ()
{
If (write (1, "hereis somedata \ n", 17 ))! = 17)
Write (2, "a write error has occurred \ n", 28 );
Exit (0); // exit () is defined in stdlib. h.
}
$ Gcc write. c-o write
$./Write
Hereis somedata
2. read system call
# Include
Size_t read (int filedes, const void * buf, size_t nbytes );
Read the bytes of the person nbytes from the file descriptor filedes associated file and put it in the Data zone buf. Returns the number of bytes actually read.
Example 2: extract the first 128 bytes of the standard input.
// Read. c
# Include
# Include
Int main ()
{
Char buffer [128];
Int nread;
Nread = read (zero, buffer, 128 );
If (nread =-1)
Write (2, "a read error has occurred \ n", 27 );
If (write (1, buffer, nread ))! = Nread)
Write (2, "a write error has occurred \ n", 28 );
Exit (0 );
}
[Root @ localhost c] # echo hello there |./read
Hello there
[Root @ localhost c] #./read <read. c
# Include
# Include
Int main ()
{
Char buffer [128];
Int nread;
Nread = read (zero, buffer, 128 );
If (nread =-1)
Write ([root @ localhost c] #
3. open System Call
# Include
# Include
# Indlude
Int open (const char * pathname, int oflags );
Int open (const char * pathname, int oflags, mode_tmode );
If the open function operation is successful, a file descriptor is returned. Pathname file path, oflags open mode, mode_tmode permission.
Oflags:
O_RDONLY open in read-only mode
Open O_WRONLY in write-only mode
Open O_RDWR in read/write mode
O_TRUNC sets the file length to zero and discards the existing content.
O_CREAT creates a file in the access mode specified in the mode.
O_EXCL and O_CREAT are used together to ensure that the file is created.
Mode_tmode:
S_IRUSR read permission, file owner
S_IWUSR write permission, file owner
S_IXUSR execution permission, file owner
S_IRGRP read permission, group of Files
S_IWGRP write permission, file group
S_IXGRP execution permission, group of Files
S_IROTH read permission, other users
S_IWOTH write permission, other users
S_IXOTH execution permission, other users
Umask variable
The umask user mask consists of three Octal numbers, which correspond to users, groups, and other users respectively.
Group other users
R W X
R prohibit reading W prohibit writing X prohibit execution
In an open call, the mode is actually a permission application. Whether the applied permission is set depends on the value of umask when the program is running.
For example, if you set your own environment to "you are not allowed to create a file that allows other users to write permission, even if you apply for a program to create the file ." However, this does not affect the addition of other permissions to a program or to users using the chmod command (or using the chmod system call in the program) in the future.
4. creat system call
# Include
# Include
# Indlude
Int creat (const char * pathname, mode_tmode );
This function is equivalent to open (pathname, O_WRONLY | O_CREAT | O_TRUNC, mode );
5. close System Call
# Include
Int close (int filedes );
The file descriptor filedes is released. Return 0: operation successful, return-1: operation failed.
6. ioctl system call
# Include
Int ioctl (int filedes, int cmd ,...);
The call to ioctl is a bit like a sack. It provides operation interfaces for Controlling Device behaviors, device descriptors, and configuration of underlying services. For more information, see the user manual for each device.
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.