fseek函数
来源:互联网 发布:阿里云备案地址 编辑:程序博客网 时间:2024/05/16 05:10
1、函数功能
用来读写一个数据块,
2、函数定义
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字节处。
程序eg:
#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;
}
注: fseek()不像lseek()会返回读写位置,因此必须使用ftell()来取得目前读写的位置。
用来读写一个数据块,
2、函数定义
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字节处。
程序eg:
#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;
}
注: fseek()不像lseek()会返回读写位置,因此必须使用ftell()来取得目前读写的位置。
- 函数:fseek()
- fseek () 函数
- fseek()函数
- fseek函数
- fseek函数
- fseek函数
- fseek函数
- fseek函数
- fseek函数
- fseek函数
- fseek函数
- fseek()函数
- fseek函数
- fseek函数
- fseek与ftell函数
- Linux 函数--fseek
- fseek()函数的说明
- Linux 函数--fseek
- POJ1953 World Cup Noise DP水题
- crc16
- (转载)gdb中汇编调试
- 利用ccstudio提供的工具解析出coff文件
- flex在creationgcompelete事件中stage为空
- fseek函数
- win+apache+php+mysql环境配置
- 用Java实现自己的数据库OR映射框架
- C#中关于水晶报表绑定dataset
- NavigatorContent visible 显示 隐藏
- 犯晕的函数指针和指针函数
- JDK1.6的JAX-WS编写WebService【转】 生成客户端代码
- 折半查找
- Windows下 emacs 用 mew 收发邮件(Gmail)