fseek()vs rewind()在C中
fseek()
C语言中的fseek()用于将文件指针移动到特定位置。偏移量和流是指针的目的地,在函数参数中给出。如果成功,则返回零,否则返回非零值。
这是fseek()C语言的语法,
int fseek(FILE *stream, long int offset, int whence)
这是在中使用的参数fseek(),
stream-这是标识流的指针。
offset-这是从位置开始的字节数。
whence-这是添加偏移量的位置。
由以下常量之一指定。
SEEK_END-文件结尾。
SEEK_SET-文件的开始。
SEEK_CUR-文件指针的当前位置。
这是fseek()C语言的示例-
假设我们具有以下内容的“demo.txt”文件-
This is demo text! This is demo text! This is demo text! This is demo text!
现在让我们看一下代码。
示例
#include<stdio.h>
void main() {
FILE *f;
f = fopen("demo.txt", "r");
if(f == NULL) {
printf("\n Can't open file or file doesn't exist.");
exit(0);
}
fseek(f, 0, SEEK_END);
printf("The size of file : %ld bytes", ftell(f));
getch();
}输出结果
The size of file : 78 bytes
在上述程序中,文件“demo.txt”是使用打开fopen()和fseek()方法用于将指针移动到文件的末尾。
f = fopen("demo.txt", "r");
if(f == NULL) {
printf("\n Can't open file or file doesn't exist.");
exit(0);
}
fseek(f, 0, SEEK_END);倒带()
该函数rewind()用于将文件的位置设置为给定流的开头。它不返回任何值。
这是rewind()C语言的语法,
void rewind(FILE *stream);
这是rewind()C语言的示例,
假设我们具有以下内容的“new.txt”文件-
This is demo! This is demo!
现在,让我们来看一个例子。
示例
#include<stdio.h>
void main() {
FILE *f;
f = fopen("new.txt", "r");
if(f == NULL) {
printf("\n Can't open file or file doesn't exist.");
exit(0);
}
rewind(f);
fseek(f, 0, SEEK_END);
printf("The size of file : %ld bytes", ftell(f));
getch();
}输出结果
The size of file : 28 bytes
在上面的程序中,使用打开文件fopen(),如果指针变量为null,则显示无法打开文件或文件不存在。该函数rewind()将指针移动到文件的开头。
f = fopen("new.txt", "r");
if(f == NULL) {
printf("\n Can't open file or file doesn't exist.");
exit(0);
}
rewind(f);热门推荐
2 成都训练狗狗
3 怎么训练小狗狗
4 训练狗狗软件
5 如何训练狗狗拉屎
9 狗狗负重训练
10 狗狗多大可以训练大小便
11 怎么训练狗狗萨摩耶
12 狗狗训练书
13 鞍山狗狗训练
14 训练狗狗用品
15 怎样才能训练狗狗听话
16 狗狗如何训练大小便
17 怎样训练狗狗睡狗窝
18 训练狗狗工具