C-language F (open) function (file operation/read/write)

Source: Internet
Author: User

Header files: #include <stdio.h>

fopen () is a common function used to open a file in a specified manner, with the following prototype:
FILE * fopen (const char * path, const char * mode);

"Parameter" path is the file name that contains the path, mode is the file open mode.

Mode is available in the following ways:

Open Mode Description
R To open a file as read-only, the file must exist.
r+ Open the file as read/write, the file must exist.
rb+ Opens a binary file in read/write mode, allowing only read/write data.
rt+ Open a text file in read/write mode, allowing read and write.
W Open a write-only file, if the file exists, the length is clear to 0, that is, the contents of the file disappears, if not present, create the file.
w+ Open a readable/writable file, and if the file exists, the file length is zero, that is, the file content disappears. If the file does not exist, the file is created.
A Open 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 data written will be added to the end of the file, that is, the original content of the file will be retained (EOF character).
A + Open a readable/writable file in an additional way. If the file does not exist, the file is created, and if the file exists, the written data is added to the end of the file, i.e. the original content of the file is retained (the old EOF character is not retained).
Wb Open or create a new binary file in write-only mode, allowing only write data.
wb+ Open or create a binary file in read/write mode, allowing both read and write.
wt+ Open or create a text file in read/write mode to allow read and write.
at+ Opens a text file in read/write mode, allowing reading or appending data at the end of the text.
ab+ Opens a binary file in read/write mode, allowing reading or appending data at the end of the file.


The b character is ignored in the POSIX system, which contains Linux. 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.

The difference between binary and text modes:

    • In the Windows system, in text mode, the file represents a newline with "\ r \ n". If you open a file in text mode and write a newline character "\ n" with a function such as fputs, the function automatically adds "\ r" before "\ n". That is, "\ r \ n" is actually written to the file.
    • In the text mode of the class Unix/linux system, the file represents a line break with "\ n". Therefore, there is no difference between the text mode and the binary mode in the Linux system.


For more information, see: C language fopen () the difference between opening a text file and a binary file

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.

When the return value file is opened successfully, the file pointer to the stream is returned. If the file fails to open, it returns null and the error code exists in errno.

Note: In general, the file will be opened after some file read or write action, if the file fails to open, the next read and write actions can not be smooth, so after fopen () Please make error judgment and processing.

After the file operation is complete, you need to close the file, be aware that this will cause the file to consume memory leaks and problems in the next access to the file.

When the file is closed, you need to point the file pointer to null, which prevents the free pointer from appearing, causing unnecessary hassles to the entire project, such as FP = NULL.

Instance opens a file and then closes the file.

      1. #include<stdio.h>
      2. #include<string.h>
      3. #include<stdlib.h>
      4. int main()
      5. {
      6. FILE* fstream;
      7. Char msg[+] = "hello! I have the read this file. ;
      8. FStream=fopen("test.txt","at+");
      9. If(fstream==null)
      10. {
      11. printf("Open file Test.txt failed! \ n");
      12. Exit(1);
      13. }
      14. Else
      15. {
      16. printf("Open file Test.txt succeed! \ n");
      17. }
      18. Fclose(FStream);
      19. return 0;
      20. }

C-language F (open) function (file operation/read/write)

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.