Home  >  Article  >  Backend Development  >  What are the ways to open files in C language?

What are the ways to open files in C language?

angryTom
angryTomOriginal
2020-03-09 14:56:2315584browse

What are the ways to open files in C language?

What are the ways to open files in C language?

ANSI C stipulates that the function fopen is used to open a file and ## is used to close it. #fclose.

1. The calling method is usually:

FILE *fp;
fp=fopen(文件名, 打开方式);

2. Parameter description:

File name: in the form of "myfile.dat", "F:\data\myfile. dat" and so on;

Recommended learning:

c language video tutorial

There are several ways to open c language files as follows:

"r"(read-only) Open a text file for input

"w"(write-only) Open a text file for output

"a"(append) to Add data to the end of the file

"rb"(read-only) Open a binary file for input

"wb"(write-only) Open a binary file for output

" r "(read-write) Open a text file for reading and writing

"w "(read-write) Create a new text file for reading and writing

"a "(read-write) Open a text file for reading Open a text file for writing

"rb "(read and write) Open a binary file for reading and writing

"wb "(read and write) Create a new binary file for reading and writing

"ab "(read and write) Open a binary file for reading and writing

3. Note:

(1) Files opened in "r" mode cannot be opened to It inputs data, and the file already exists, otherwise an error occurs;

(2) For a file opened in "w" mode, data can only be input to the file. If the file being opened does not exist, it will be opened after Create a new file named with the specified name; if the specified file exists, the file will be deleted when opening, and then a new file will be created;

(3) If the function fopen makes an error when opening the file, then fopen returns a null pointer value NULL;

(4) When the program starts running, the system automatically opens three standard files: standard input (stdin), standard output (stdout), and standard error output (stderr). If you want to use the input and output terminal, you do not need to open it. You can use it directly, such as fputc(stdout,'a'); to output the character a to the screen.

For more

Introduction to Programming tutorials, please pay attention to the PHP Chinese website!

The above is the detailed content of What are the ways to open files in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn