c语言中文件输入/输出
A file represents a sequence of bytes on the disk where a group of related data is stored. File is created for permanent storage of data. It is a ready made structure.
文件表示磁盘上存储一组相关数据的字节序列。 创建文件以永久存储数据。 它是现成的结构。
In C language, we use a structure pointer of file type to declare a file.
在C语言中,我们使用文件类型的结构指针来声明文件。
FILE *fp;
C provides a number of functions that helps to perform basic file operations. Following are the functions,
C提供了许多有助于执行基本文件操作的功能。 以下是功能,
Function | description |
---|---|
fopen() | create a new file or open a existing file |
fclose() | closes a file |
getc() | reads a character from a file |
putc() | writes a character to a file |
fscanf() | reads a set of data from a file |
fprintf() | writes a set of data to a file |
getw() | reads a integer from a file |
putw() | writes a integer to a file |
fseek() | set the position to desire point |
ftell() | gives current position in the file |
rewind() | set the position to the begining point |
功能 | 描述 |
---|---|
fopen() | 创建一个新文件或打开一个现有文件 |
fclose() | 关闭文件 |
getc() | 从文件中读取字符 |
putc() | 将字符写入文件 |
fscanf() | 从文件中读取一组数据 |
fprintf() | 将一组数据写入文件 |
getw() | 从文件中读取一个整数 |
putw() | 将整数写入文件 |
fseek() | 将位置设置为期望点 |
ftell() | 给出文件中的当前位置 |
倒带() | 将位置设置为起点 |
打开文件或创建文件 (Opening a File or Creating a File)
The fopen()
function is used to create a new file or to open an existing file.
fopen()
函数用于创建新文件或打开现有文件。
General Syntax:
通用语法:
*fp = FILE *fopen(const char *filename, const char *mode);
Here, *fp
is the FILE pointer (FILE *fp
), which will hold the reference to the opened(or created) file.
此处