
fseek()在C语言中用于将文件指针移动到特定位置。偏移量和流是指针的目标,它们在函数参数中给出。如果成功,它返回零。如果不成功,它返回非零值。
以下是C语言中fseek()的语法:
int fseek(FILE *stream, long int offset, int whence)
这里是在fseek()中使用的参数:
stream − 这是用于标识流的指针。
立即学习“C++免费学习笔记(深入)”;
offset − 这是从位置开始的字节数。
whence − 这是偏移量添加的位置。
whence由以下常量之一指定。
SEEK_END − 文件末尾。
SEEK_SET − 文件开头。
SEEK_CUR − 文件指针的当前位置。
这是C语言中fseek()的一个示例。
假设我们有一个名为“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
以上就是在C/C++中,fseek()函数用于在文件中移动文件指针的位置的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号