Difference between fopen/open, read/write and fread/fwrite

Source: Internet
Author: User
Tags fread


Differences between fopen and open

In UNIX, C has two sets of reading and writing binary stream files: 1) fopen, fread, fwrite; 2) Open, read, write
Here is a brief introduction of their differences.
1. The fopen series are standard C library functions, and the Open series are defined by POSIX and are system calls in UNIX systems.
That is to say, the fopen series are more portable, while the Open series can only be used in POSIX operating systems.
2. when using fopen functions, you must define an object that refers to a file. It is called "file handler" and is a struct; the Open series uses an int integer called "file descriptor.
3. The fopen series are high-level I/O, and buffer is used for reading and writing. The Open series are relatively low-level, closer to the operating system, and there is no buffer for reading and writing. Because they can deal with more operating systems, the Open series can access and change some information that cannot be accessed by the fopen series, such as viewing the read and write permissions of files. These additional features are generally system-specific.
4. "# include <sdtio. h> "; to use open functions, you need to" # include <fcntl. h> ", use libc (-LC) for Link)
Summary:
In general, in order to make the program more portable, The fopen series is the first choice without having to use features that cannot be implemented by the fopen series.

Difference between read/write and fread/fwrite

1. fread is buffered, and read is not buffered.
2. fopen is defined in standard C, and open is defined in POSIX.
3. fread can read a structure. There is no difference between reading binary files in Linux/Unix.
4. fopen cannot specify the permission to create a file. Open can specify the permission.
5. fopen returns the pointer and open returns the file descriptor (integer ).
6. If any device in Linux/Unix is a file, you can use open or read.
If the file size is 8 K.
If you use read/write and only allocate 2 k cache, You need to perform four system calls to read the file from the disk.
If you use fread/fwrite, the system automatically allocates the cache and reads the file as long as one system call is read from the disk.
That is, read/write is used to read the disk four times, while fread/fwrite is used to read the disk only once. It is 4 times more efficient than read/write.
If the program has a limit on the memory, it is better to use read/write.
Fread and fwrite are used to automatically allocate the cache, which is faster and easier than doing it on your own. If you want to process some special descriptors, use read and write, such as a set of interfaces and pipelines.
The efficiency of calling write by the system depends on the size of your Buf and the total number of writes you want to write. If the Buf is too small, the number of times you enter the kernel space increases greatly, resulting in low efficiency. Fwrite caches the data for you to reduce the number of system calls that actually occur, so the efficiency is relatively high.
If it is only called once (is it possible ?), The two are similar. Strictly speaking, write is a little faster (because fwrite actually uses write to write the file system), but the difference does not matter.

------------------

 

Header file

# Include <stdio. h>

Define functions

File * fopen (const char * path, const char * mode );

Function Description

The parameter path string contains the path and file name of the file to be opened. The parameter mode string represents the stream format.
Mode has the following forms of strings:
R: open a read-only file, which must exist.
R + open a readable file, which must exist.
W. Open and write only the file. If the file exists, the file length is 0, indicating that the file content will disappear. If the file does not exist, the file is created.
W + open the readable and writable file. If the file exists, the file length is cleared to zero, that is, the file content disappears. If the file does not exist, the file is created.
A. Open and write-only files as an attachment. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained.
A + opens readable and writable files by appending them. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained.
The preceding morphological string can be added with a B character, such as a combination of Rb, W + B, and AB +. The B character is added to indicate that the file opened by the function library is a binary file, rather than text files. However, this character is ignored in the POSIX System in Linux. The new file created by fopen () has the s_irusr | s_iwusr | s_irgrp | s_iwgrp | s_iroth | s_iwoth (0666) Permission. For the File Permission, see umask value.

Return Value

After the file is successfully opened, the file pointer pointing to the stream will be returned. If the file fails to be opened, null is returned and the error code is stored in errno.

Additional instructions

In general, after opening a file, some file reading or writing operations will be performed. If opening a file fails, the subsequent read/write operations will not proceed smoothly. Therefore, in the fopen () then, make error judgment and handling.

 

Header file

# Include <stdio. h>

Define functions

Int fclose (File * stream );

Function Description

Fclose () is used to close files opened by fopen. This action writes data in the buffer zone to a file and releases the file resources provided by the system.

Return Value

If the file is successfully closed, 0 is returned. If an error occurs, EOF is returned and the error code is saved to errno.

Error Code

Ebadf indicates that the parameter stream is not an open file.

Fread (reads data from a file Stream)

Related functions

Fopen, fwrite, fseek, fscanf

Header file

# Include <stdio. h>

Define functions

Size_t fread (void * PTR, size_t size, size_t nmemb, file * stream );

Function Description

Fread () is used to read data from a file stream. The stream parameter is an opened file pointer. the PTR parameter points to the data space to be read. The number of characters read is determined by the size * nmemb parameter. Fread () returns the number of nmemb actually read. If this value is smaller than the nmemb parameter, it indicates that the file may be read at the end or an error occurs. In this case, feof () must be used () or ferror () to determine what happens.

Return Value

Returns the number of nmemb instances actually read.

Fwrite (write data to a file Stream)

Related functions

Fopen, fread, fseek, fscanf

Header file

# Include <stdio. h>

Define functions

Size_t fwrite (const void * PTR, size_t size, size_t nmemb, file * stream );

Function Description

Fwrite () is used to write data into the file stream. The stream parameter is an opened file pointer. the PTR parameter points to the data address to be written. The total number of characters written is determined by the parameter size * nmemb. Fwrite () returns the number of nmemb actually written.

Return Value

Returns the number of nmemb actually written.


Source: http://blog.csdn.net/fuyjlu/article/details/5508421


Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

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.