fopen and read-write identification r,r+,rb+,rt+,w+ .....

Source: Internet
Author: User
Introduction to Functions

  function function: open a file

  function Prototypes: FILE * fopen (const char * path,const char * mode);

related functions:open,fclose,fopen_s[1]   , _wfopen

  Required libraries: <stdio.h>

  return value: After the file is successfully opened, the file pointer to the stream is returned. Returns null if the file fails to open, and the error code exists in errno.

Generally speaking, the file will be opened after some files read or write the action, if the file failed to open, the next reading and writing can not be smoothly, so in the fopen () after the error judgment and processing.

  parameter Description:

The parameter path string contains the file path and filename you want to open, and the parameter mode string represents the flow pattern.

Mode has the following morphological strings:

R to open a read-only file, the file must exist.

r+ open a file that can be read and write, the file must exist.

rb+ Read and write to open a binary file, allowing only read and write data.

rt+ read-Write opens a text file that allows reading and writing.

W Open Write-only file, if the file exists, the file length is 0, that is, the file content will disappear. If the file does not exist, the file is created.

w+ open a writable file, if the file exists, the file length is zero, that is, the file content will disappear. If the file does not exist, the file is created.

A opens the write-only file in an additional way. If the file does not exist, the file will be created, and if the file exists, the written data will be added to the end of the file, that is, the original contents of the file will be retained. (The EOF character is reserved)

A + opens the writable file in an additional way. If the file does not exist, the file will be created, and if the file exists, the written data will be added to the end of the file, that is, the original contents of the file will be retained. (the original EOF character is not retained)

WB write-only open or create a binary file;

wb+ Read and write to open or create a binary file that allows reading and writing.

wt+ Read and write to open or create a text file;

at+ read-Write opens a text file that allows you to read or append data at the end of the text.

ab+ read-write opens a binary file that allows you to read or append data at the end of a file.

The above morphological strings can be added to a B-character, such as RB, W+b or ab+, add a B-character to tell the function library to open the file as a binary file, not a plain text file. However, in the POSIX system, including Linux ignores the character. The new file created by fopen () will have a s_irusr| s_iwusr| s_irgrp| s_iwgrp| s_iroth| S_iwoth (0666) permission, this file permission will also refer to the Umask value.

Some c-compiler systems may not fully provide all of these functions, some C version without "r+", "w+", "A +", and with "RW", "WR", "AR" and so on, readers pay attention to the provisions of the system.

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.