The difference between Python Fopen,open, and Popen

Source: Internet
Author: User

1. fopen  
    • Open Normal file
    • Sprinkle with a little bit of powder in the buffer area
    • Buffer file system is the use of file structure pointer to manage the file, through the file pointer to access the file, can read and write characters, strings, formatted data, can also read and write binary data

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

Required library:<stdio.h> return value: When the file is opened successfully, the file pointer to the stream is returned. Returns null if the file fails to open, and the error code exists in errno. Parameter description: The parameter path string contains the file paths and filenames to open, and the parameter mode string represents the flow pattern. Mode has the following morphological strings: R opens the file as read-only and the file must exist. r+ Open the file as read-write, the file must be present. rb+ Read and write open a binary file that allows you to read and write data. rw+ Read and write open a text file that allows reading and writing. W Open Write-only file, if the file exists then the file length is clear to 0, that is, the contents of the file will disappear. If the file does not exist, the file is created. w+ Open a read-write file, if the file exists then the file length is clear to zero, that is, the contents of the file will disappear. If the file does not exist, the file is created. A write-only file opens in an additional way. If the file does not exist, the file will be created, and if the file exists, the data written will be added to the end of the file, that is, the original content of the file will be retained. (EOF character reserved) A + opens a read-write file in an additional way. If the file does not exist, the file will be created, and if the file exists, the data written will be added to the end of the file, that is, the original content of the file will be retained. (the original EOF character is not retained) WB only writes Open or creates a new binary file; Only write data is allowed. wb+ read-write open or create a binary file that allows reading and writing. ab+ Read and write open a binary file that allows you to read or append data at the end of the file. at+ open a file called string, a means append, that is, when the writing process is followed by the original file has been written, not written from scratch, t means that the type of open file is a text file, + number indicates that the file can be read or write. These morphological strings can be added with a B character, such as RB, W+b, or ab+, and a B character is used to tell the library to open the file in binary mode. If you do not add B, the default is to add T, which is RT,WT, where T indicates that the file is opened in text mode. A 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 features, and some C versions do not use "r+", "w+", "A +", but with "RW", "WR", "AR" and so on, readers pay attention to the rules of the system used.   2. Open
    • Open the device file
    • With no buffers
    • Non-buffered file system relies on the operating system, through the function of the operating system to read and write files, is a system-level input and output, it does not set the file structure pointer, only read and write binary files, but high efficiency, fast
 Function prototypes: int open (const char *pathname, int flags); int open (const char *pathname, int flags, mode_t mode); Required library:<fcntl.h> return value: Success returns the file descriptor, otherwise 1; parameter description: For the Open function, the third parameter is used only when a new file is created (that is, when o_creat is used) to specify the access permission bit for the file (access Permission bits). Pathname is a POSIX pathname (such as/home/user/a.cpp) to open/create a file, flags is used to specify the open/create mode of the file, which can be composed of the following constants (defined in fcntl.h) through logical bits or logic.
    1. O_RDONLY 只读模式
    2. O_WRONLY 只写模式
    3. O_RDWR 读写模式
When opening/creating a file, use at least one of the three constants above. The following constants are selected:
    • O_APPEND 每次写操作都写入文件的末尾
    • O_CREAT 如果指定文件不存在,则创建这个文件
    • O_EXCL 如果要创建的文件已存在,则返回 -1,并且修改 errno 的值
    • O_TRUNC 如果文件存在,并且以只写/读写方式打开,则清空文件全部内容(即将其长度截短为0)
    • O_NOCTTY 如果路径名指向终端设备,不要把这个设备用作控制终端。
    • O_NONBLOCK 如果路径名指向 FIFO/块文件/字符文件,则把文件的打开和后继 I/O
Set to non-blocking mode
    • (nonblocking mode)
The following three constants are also selected for synchronous input and output
    • O_dsync wait for physical I/O to finish before write. Does not wait for the file property to be updated without affecting the reading of the newly written data.
    • O_RSYNCread等待所有写入同一区域的写操作完成后再进行
    • O_SYNC 等待物理 I/O 结束后再write,包括更新文件属性的 I/O
3.popen
    • Open pipe
Function Prototypes: FILE * Popen (const char * command, const char * type), required library:<stdio.h> return value: If the call to fork () or pipe () fails, or the memory cannot be allocated will return n ULL, otherwise return the standard I/O stream. Parameter description:typeParameters can only be read or write, and the resulting return value (standard I/O stream) also has a read-only or write-only type corresponding to type. If type is "R" then the file pointer is connected to the standard output of command, and if type is "W" then the file pointer is connected to the standard input of command.The command parameter is a pointer to a NULL-terminated shell command string.Pointer. This line of command will be passed to bin/sh and use the-c flag and the shell will execute the command.

The difference between Python Fopen,open, and Popen

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.