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);