Linux environment Programming-file basic operations

Source: Internet
Author: User
Tags function prototype printf

Linux under the directory is/such and how does Windows remember? and W is not the same as the reverse? So the Linux directory is reverse, so remember.

  One: The Open function

Name: Open

Goal: Open a file.

Header file: #include

#include

#include < fcntl.h>

Function prototype: int open (const char * pathname,int flags);

int open (const char * pathname,int flags,mode_t mode);

Parameters: Pathname file name

Flags open Mode

return value:-1 encountered an error

The int opens successfully and returns the file descriptor.

This system call establishes a connection between the process and the file, which is called a file descriptor and is like a pipeline to the kernel by the process.

To open a file, you must specify a filename and open mode, with 3 open modes: Read-only, write-only, readable-writable, corresponding to O_RDONLY,O_WRONLY,O_RDWR, which is defined in the header file/usr/include/fcntl.h.

Opening a file is a service provided by the kernel, and if any errors are detected by the kernel during the opening process, the system call returns-1. The type of error is various, such as: The file to be opened does not exist. Even if a file exists that cannot be opened because of insufficient permissions, you can look at the various possible errors listed in the Open online Help.

UNIX allows a file to be accessed by multiple processes, meaning that when a file is opened by a process, it can also be opened by another process.

If the file is successfully opened the kernel returns a positive integer value, this number is called a file descriptor, which is a simple integer that identifies each file that is opened by the process, the descriptor 0 represents the standard output, the corresponding macro is Stdout_fileno, and the descriptor 1 represents the standard input. The corresponding macro is Stdin_fileno, the descriptor 2 represents the standard error output, the corresponding macro is Stderr_fileno, the system assigns the process assignment descriptor from 3, and if several files are opened at the same time, their corresponding file descriptors are different, if a file is opened several times, The corresponding file descriptor is also different. You must manipulate the file through a file descriptor. The following procedure can prove this.

  Two: Read function

Name: Read

Target: Read the data to the buffer.

Header files: #include < unistd.h>

Function prototype: ssize_t read (int fd, void *buf, size_t count)

Parameters: FD File descriptor

BUF the destination buffer used for storing data

Number of bytes to read by count

return value:-1 encountered an error

Numread closed successfully, returning the number of bytes read.

Read this system call request kernel reads the Qty Byte data from the file specified by FD, and stores it in the memory space specified by BUF, if the kernel succeeds in reading the data, it returns the number of bytes read. otherwise return-1.

When the number of bytes in the file is not as long as you want, read will determine if the next value is not ", stop reading If it is, and then exit." Numread returns the number of bytes before, that is, the number of bytes in the original file rather than the number of bytes you want to read.

  Three: the Close function

Name: Close

Goal: Close a file.

Header files: #include < unistd.h>

Function prototype: int close (int fd)

Parameters: FD File descriptor

return value:-1 encountered an error

int closed successfully, returns the file descriptor.

Close this system call closes the connection between the process and the file FD, and if an error occurs during shutdown, close returns-1, such as: The file that FD refers to does not exist. Successful shutdown returns the file descriptor.

Code: an example of a basic open file, read, and close

#include

#include

#include

#include

#include

#include

Main ()

{

int fd,size;

char s []= "Linux programmer!n", buffer[1024];

Fd=open ("Show_read.c", o_rdonly);

Size=read (fd,buffer,sizeof (buffer));

Close (FD);

printf ("%s", buffer);

}

  Four: Craet function

Name: creat

Goal: Create/rewrite a file

Header file: #include

#include

#include < fcntl.h>

Function prototype: int creat (const char *pathname,mode_t mode)

Parameters: Pathname file name

Mode access modes

return value:-1 encountered an error

FD created successfully, return file descriptor

Creat tells the kernel to create a file named filename, if it doesn't exist, create it, if it already exists, empty its contents and set the file length to 0.

If the kernel succeeds in creating the file, the file's permission bit (permission bits) is set to the value specified by the second argument mode. such as:

Fd=creat ("AddressBook", 0644);

Create a file named AddressBook, and if the file does not exist, the file's permission bit is set to rw-r-r-.

If the file already exists its contents will be emptied. In either case, FD will be a file descriptor pointing to AddressBook.

  Five: Write function

Name: Write

Target: Writes the data in memory to the file.

Header files: #include < unistd.h>

Function prototype: size_t write (int fd, const void *buf, size_t count)

Parameters: FD File descriptor

BUF Memory Data

Number of bytes to write for count

return value:-1 encountered an error

Num written writes successfully, returning the number of bytes written.

During the actual write process, the number of bytes written may appear to be less than required. There may be two reasons for this, the first is that some systems have a limit on the maximum size of the file, and the second is that disk space is nearly full. In both cases the kernel tries to write the data to the file and returns the actual number of bytes written, so calling write must check to see if the return value is the same as the write, and if it is different, take the appropriate action.

After learning the above several system calls, we can write our own CP command. Its basic idea is to read data from the original file to buffer, and then write the buffered data to the target file.

Code: Create a new file and write to the content

#include

#include

#include

#include

#include

#include

Main ()

{

int fd,size;

char s []= "Linux programmer!n", buffer[1024];

Char filename[]= "Newfile.txt";

Fd=creat (filename,0644);

Fd=open (filename,o_wronly| O_creat);

Write (fd,s,sizeof (buffer));

Fd=open (filename,o_rdonly);

Size=read (fd,buffer,sizeof (buffer));

Close (FD);

printf ("%s", buffer);

}

Related Article

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.