File read and write in win environment

Source: Internet
Author: User
Tags control characters

In the win environment, there are many ways to read and write files, such as the CFile class in MFC, and some open source projects such as Qt Qfile. Open source is good to be multi-platform, and MFC is only Microsoft's own things, for those who want to write cross-platform, it is best not to use MFC.

Recently in the writing development, suddenly encountered a problem, is also related to read and write files, but with the C method, rather than C + +, the problem is the expression of C in the open file is read-only, usually rarely used this method on the Internet to find the next, only to find that the function has a permission parameter, the default is read-only. Now the C mode of the two file operations under the induction

Open is lower-level IO operation than fopen, so the program requires more manual operation

int open (const char *path, int access,int mode)

Path and name of file to open path

Access mode, macro definition and meaning are as follows:
O_rdonly 1 Read-only Open
O_wronly 2 Write-only Open
O_rdwr 4 Read and write Open
The following modes can also be selected with the above 3 basic modes:
O_creat 0x0100 Create a file and open
O_trunc 0x0200 Open an existing file and set the file length to 0, other properties remain
O_EXCL 0x0400 not used
O_append 0x0800 Append Open File
O_text 0x4000 Open Text file translate CR-LF control characters
O_binary 0x8000 Open binary character, do not make CR-LF translation
Mode This parameter is used only in the Access=o_creat mode, and its value is as follows:
S_ifmt 0xf000 File Type mask
S_ifdir 0x4000 Directory
S_ififo 0x1000 FIFO dedicated
S_IFCHR 0x2000 Character-specific
S_IFBLK 0x3000 Block Dedicated
S_ifreg 0x8000 only for 0x0000
S_iread 0x0100 Readable
S_iwrite 0x0080 Writable
S_iexec 0x0040 Executable
FILE *fopen (Char *filename, char *mode)
FileName File name
Mode Open:
R Read-only mode to open a text file
RB read-only mode opens a binary file
W write-only method to open a text file
WB Write-only mode opens a binary file
A Append method to open a text file
AB Append method open a binary file
r+ open a text file in a readable and writable manner
rb+ readable writable open a binary file
w+ to create a text file in a readable and writable manner
wb+ A read-write way to generate a binary file
A + read-write Append method to open a text file
ab+ to append a binary file in a readable and writable manner
The difference between open and fopen:
The former belongs to low-level IO, which is advanced IO.
The former returns a file descriptor, which returns a file pointer.
The former has no buffering and the latter is buffered.
The former and read, write and so on with the use of the latter with Fread, fwrite and so on with the use.
The latter is based on the former, in most cases, with the latter.

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.