C语言-文件操作

本文详细介绍了C语言中四个重要的文件操作函数:rewind用于将文件指针重置到文件开头;feof用于检测文件是否到达结尾,返回非零值表示已到文件末尾;ftell获取当前文件位置;fgets用于从文件中读取一行数据,直至遇到换行符或缓存满。这些函数是进行文件操作的基础,对于理解和处理文件至关重要。

1.总览

Index函数名用途描述与说明返回值
1rewind设置文件位置指示器到文件的开头,等价于(void) fseek(stream, 0L, SEEK_SET)没有返回值
2feof测试指向的流中的文件结束标记,如果已设置就返回非零值。这个函数不应当失败,它不设置外部变量 errno 。(但是,如果 fileno 检测到它的参数不是有效的流,它必须返回 -1,并且将 errno 设置为 EBADF 。)
3ftell获取当前文件位置指示若成功则为当前文件位置指示,若出错则为-1L
4fgets

fgets函数读入数据,直到行结束或缓存满(当然会留出一个字节存放终止字符).

2.分述

2.1 rewind

#include <stdio.h>
void rewind(FILE *stream);
The rewind() function sets the file position indicator for the stream pointed to by 
stream to the beginning of the file.  It is equivalent to: 
(void) fseek(stream, 0L, SEEK_SET)
The rewind() function returns no value.

2.2 feof

tests the end-of-file indicator for the stream pointed to by stream.
测试指向的流中的文件结束标记,如果已设置就返回非零值。文件结束标记只能用函数clearerr清除.

2.3 ftell

The ftell() function obtains the current value of the file position indicator for the 
stream pointed to by stream.


The rewind() function returns no value.  Upon successful completion, fgetpos(), 
fseek(),fsetpos() return 0, and ftell() returns the  current  offset.  Otherwise,
-1 is returned and errno is set to indicate the error.

2.4 fgets

#include <stdio.h>
char *fgets(char *s, int size, FILE *stream);

fgets() reads in at most one less than size characters from stream and stores them 
into the buffer pointed to by s.  Reading stops after an EOF  or a newline.  
If a newline is read, it is stored into the buffer.  A terminating null byte ('\0') 
is stored after the last character in the buffer.

gets() and fgets() return s on success, and NULL on error or when end of file occurs 
while no characters have been read.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值