首页技术fseek函数使用方法?fgetc函数

fseek函数使用方法?fgetc函数

编程之家2026-05-271006次浏览

大家好,感谢邀请,今天来为大家分享一下fseek函数使用方法的问题,以及和fgetc函数的一些困惑,大家要是还不太明白的话,也没有关系,因为接下来将为大家分享,希望可以帮助到大家,解决大家的问题,下面就开始吧!

fseek函数使用方法?fgetc函数

fseek函数是怎么用的

fseek函数用于重定位流(数据流/文件)上的文件内部位置指针。

函数原型int fseek(FILE*stream, long offset, int fromwhere);

参数:

stream为文件流的指针

offset偏移量

fromwhere起始位置

fseek函数使用方法?fgetc函数

具体功能:如果执行成功,stream将指向以fromwhere(起始位置:文件头0(SEEK_SET),当前位置1(SEEK_CUR),文件尾2(SEEK_END))为基准,偏移offset(指针偏移量)个字节的位置。如果执行失败(比如offset超过文件自身大小),则不改变stream指向的位置。

返回值:

成功,返回0,失败返回-1,并设置errno的值,可以用perror()函数输出错误。

实例:

#include<stdio.h>

long filesize(FILE*stream);

fseek函数使用方法?fgetc函数

int main(void)

{

FILE*stream;

stream=fopen("MYFILE.TXT","w+");

fprintf(stream,"This is a test");

printf("File size of MYFILE.TXT is%ld bytes\n",filesize(stream));

fclose(stream);

return 0;

}

long filesize(FILE*stream)

{

long curpos,length;

curpos=ftell(stream);

fseek(stream,0L,SEEK_END);

length=ftell(stream);

fseek(stream,curpos,SEEK_SET);

return length;

}

函数fseek()的正确调用形式是( )。

【答案】:B

指针函数fseek()的调用形式为:fseek(fp,offset,position);fseek函数参数说明:“fp”是指向该文件的文件型指针;“offset”为位移量,是指从起始点position到要确定的新位置的字节数。以起点为基准,向前移动字节数。“position”为起始点,指出以文件的什么位置为基准进行移动,position的值用整型常数表示,“0”表示文件的开头,“1”表示文件的当前位置,“2”表示文件的末尾。

fseek(f,0,SEEK_SET);

意思是把文件指针指向文件的开头

fseek

函数名: fseek

功能:重定位流上的文件指针

用法: int fseek(FILE*stream, long offset, int fromwhere);

描述:函数设置文件指针stream的位置。如果执行成功,stream将指向以fromwhere为基准,偏移offset个字节的位置。如果执行失败(比如offset超过文件自身大小),则不改变stream指向的位置。

返回值:成功,返回0,否则返回其他值。

fseek position the file position pointer for the file referenced by stream to the byte location calculated by offset.

程序例:

#include<stdio.h>

long filesize(FILE*stream);

int main(void)

{

FILE*stream;

stream= fopen("MYFILE.TXT","w+");

fprintf(stream,"This is a test");

printf("Filesize of MYFILE.TXT is%ld bytes\n", filesize(stream));

fclose(stream);

return 0;

}

long filesize(FILE*stream)

{

long curpos, length;

curpos= ftell(stream);

fseek(stream, 0L, SEEK_END);

length= ftell(stream);

fseek(stream, curpos, SEEK_SET);

return length;

}

int fseek( FILE*stream, long offset, int origin);

第一个参数stream为文件指针

第二个参数offset为偏移量,整数表示正向偏移,负数表示负向偏移

第三个参数origin设定从文件的哪里开始偏移,可能取值为:SEEK_CUR、 SEEK_END或 SEEK_SET

SEEK_SET:文件开头

SEEK_CUR:当前位置

SEEK_END:文件结尾

其中SEEK_SET,SEEK_CUR和SEEK_END和依次为0,1和2.

简言之:

fseek(fp,100L,0);把fp指针移动到离文件开头100字节处;

fseek(fp,100L,1);把fp指针移动到离文件当前位置100字节处;

fseek(fp,100L,2);把fp指针退回到离文件结尾100字节处。

使用实例:

#include<stdio.h>

#define N 5

typedef struct student{

long sno;

char name[10];

float score[3];

} STU;

void fun(char*filename, STU n)

{

FILE*fp;

fp= fopen(filename,"rb+");

fseek(fp,-1L*sizeof(STU),SEEK_END);

fwrite(&n, sizeof(STU), 1, fp);

fclose(fp);

}

void main()/*修改覆盖最后一个学生数据*/

{

STU t[N]={{10001,"MaChao", 91, 92, 77},{10002,"CaoKai", 75, 60, 88},

{10003,"LiSi", 85, 70, 78},{10004,"FangFang", 90, 82, 87},

{10005,"ZhangSan", 95, 80, 88}};

STU n={10006,"ZhaoSi", 55, 70, 68}, ss[N];

int i,j; FILE*fp;

fp= fopen("student.dat","wb");

fwrite(t, sizeof(STU), N, fp);

fclose(fp);

fp= fopen("student.dat","rb");

fread(ss, sizeof(STU), N, fp);

fclose(fp);

printf("\nThe original data:\n\n");

for(j=0; j<N; j++)

{

printf("\nNo:%ld Name:%-8s Scores:",ss[j].sno, ss[j].name);

for(i=0; i<3; i++) printf("%6.2f", ss[j].score[i]);

printf("\n");

}

fun("student.dat", n);

printf("\nThe data after modifing:\n\n");

fp= fopen("student.dat","rb");

fread(ss, sizeof(STU), N, fp);

fclose(fp);

for(j=0; j<N; j++)

{

printf("\nNo:%ld Name:%-8s Scores:",ss[j].sno, ss[j].name);

for(i=0; i<3; i++) printf("%6.2f", ss[j].score[i]);

printf("\n");

}

}

fseek函数使用方法的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于fgetc函数、fseek函数使用方法的信息别忘了在本站进行查找哦。

源代码1080p下载?源码编辑器官方下载ai项目怎么做?如何用ai写项目文档