原文地址:fseek()作者:xiaoxin

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

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 bytesn",
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 :nn");
  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 :nn");
  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()的更多相关文章

  1. FILE文件流的中fopen、fread、fseek、fclose的使用

    FILE文件流用于对文件的快速操作,主要的操作函数有fopen.fseek.fread.fclose,在对文件结构比较清楚时使用这几个函数会比较快捷的得到文件中具体位置的数据,提取对我们有用的信息,满 ...

  2. C中的fseek函数使用

    函数名:fseek函数 头文件:#include<stdio.h> 功能:把与fp有关的文件位置指针放到一个指定位置. 格式:  int fseek(FILE *stream, long ...

  3. Linux C 文件输入输出函数 fopen()、getc()/fgetc()、putc()/fputc()、fclose()、fprintf()、fscanf()、fgets()、fputs()、fseek()、ftell()、fgetpos()、fsetpos() 详解

      fopen(打开文件) 定义函数 FILE * fopen(const char * path,const char * mode); 函数说明 参数path字符串包含欲打开的文件路径及文件名,参 ...

  4. php使用file函数、fseek函数读取大文件效率分析

    php读取大文件可以使用file函数和fseek函数,但是二者之间效率可能存在差异,本文章向大家介绍php file函数与fseek函数实现大文件读取效率对比分析,需要的朋友可以参考一下. 1. 直接 ...

  5. PHP移动文件指针ftell()、fseek()、rewind()总结

    在对文件进行读写过程中,有时需要在文件中跳转.同不同位置读取,以及将数据写入到不同的位置.例如,使用文件模拟数据库保存数据,就需要移动文件指针.指针的位置是以从文件头开始的字节数度量的,默认以不同模式 ...

  6. 函数fseek() 用法(转)

    在阅读代码时,遇到了很早之前用过的fseek(),很久没有用了,有点陌生,写出来以便下次查阅. 函数功能是把文件指针指向文件的开头,需要包含头文件stdio.h fseek   函数名: fseek ...

  7. C语言 文件操作11--文件函数再讲 fseek()和ftell()

    //文件函数再讲 //fseek(),ftell(), #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdl ...

  8. matlab文件操作及读txt文件(fopen,fseek,fread,fclose)

    文件操作是一种重要的输入输出方式,即从数据文件读取数据或将结果写入数据文件.MATLAB提供了一系列低层输入输出函数,专门用于文件操作. 1.文件的打开与关闭 1)打开文件 在读写文件之前,必须先用f ...

  9. Matlab txt内容替换函数 fgetl fseek

    Data Import and Export  :Low-Level File I/O the contents of the file:    16     5     9     4     2  ...

  10. fseek的使用

    一:概述 在官方文档里,对于fseek的描述是 Move to specified position in file,移到文件的某一个特殊位置 二:语法 status = fseek(fileID, ...

随机推荐

  1. 使用libCurl实现断点下载

    关键部分代码如下: #include "curl.h" #pragma comment(lib, "libcurl.lib") size_t CROS_Down ...

  2. 控件构造函数需要的AOwner是TComponent,而不是Parent

    普通控件都只有一个构造函数,但是这个构造函数却强迫指定AOwner,也就是说,VCL希望将所有控件(至少是所有可视化控件)全部置于它的管理之下.至于到底显示不显示,那是另一个层次的问题.这个问题其实挺 ...

  3. 一个优秀的http实现框架

    package com.ming; import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.Unir ...

  4. VS2013 ASP.NET MVC 修改Web项目的IISExpress的端口固定

    [首先]关闭防火墙,或防火墙开放端口  在解决方案中,右键某项目,属性——Web——服务器——选择IISExpress URL输入:http://localhost:8000/   直接将8000更改 ...

  5. 【HDOJ】1160 FatMouse's Speed

    DP. #include <stdio.h> #include <string.h> #include <stdlib.h> #define MAXNUM 1005 ...

  6. bzoj2553

    似乎挂精度了,不过这是一道好题 很明显看题知算法,知道这道题肯定是AC自动机上矩阵乘法 首先要明确一点,对一个字符串,怎样划分禁忌串最多 根据求最多不相交线段可知,从头到尾能划分出禁忌串就划分 根据这 ...

  7. Master Nginx(1) - Installing Nginx and Third-Party Modules

    Installing NGINX and Third-Party Modules Installing Nginx using a package manager Linux(deb-based) s ...

  8. 大白书 209 remember the word

    F - Remember the Word Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Sub ...

  9. Bzoj 1975: [Sdoi2010]魔法猪学院 dijkstra,堆,A*,K短路

    1975: [Sdoi2010]魔法猪学院 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1357  Solved: 446[Submit][Statu ...

  10. 5 个在 Linux 中管理文件类型和系统时间的有用命令

    对于想学习 Linux 的初学者来说要适应使用命令行或者终端可能非常困难.由于终端比图形用户界面程序更能帮助用户控制 Linux 系统,我们必须习惯在终端中运行命令.因此为了有效记忆 Linux 不同 ...