fseek库函数

  1. #include <stdio.h>
  2. int fseek(FILE *stream, long int offset, int origin);
  3. 返回:成功为0,出错为非0

对流stream相关的文件定位,随后的读写操作将从新位置开始。

对于二进制文件,此位置被定位在由origin开始的offset个字符处。origin的值可能为SEEK_SET(文件开始处)、SEEK_CUR(当前位置)或SEEK_END(文件结束处)。

对于文本流,offset心须为0,或者是由函数ftell()返回的值(此时origin的值必须是SEEK_SET)(这里关于与ftell函数的交互,不是很理解。)。

ftell库函数

  1. #include <stdio.h>
  2. long int ftell(FILE *stream);

返回与流stream相关的文件的当前位置。出错时返回-1L。

fflush库函数

  1.  
  1. #include <stdio.h>
  2. int fflush(FILE *stream);
  3. 返回:成功为0,失败返回EOF

对输出流(写打开),fflush()用于将已写到缓冲区但尚未写出的全部数据都写到文件中;对输入流,其结果未定义。如果写过程中发生错误则返回EOF,正常则返回0。

fflush(NULL)用于刷新所有的输出流。

程序正常结束或缓冲区满时,缓冲区自动清仓。

 lseek库函数

  1. 头文件:#include <sys/types.h> #include <unistd.h>
  2.  
  3. 定义函数:off_t lseek(int fildes, off_t offset, int whence);

lseek函数不是ANSI C标准库函数,只是满足POSIX的UNIX下的函数。

函数说明:
  每一个已打开的文件都有一个读写位置, 当打开文件时通常其读写位置是指向文件开头, 若是以附加的方式打开文件(如O_APPEND), 则读写位置会指向文件尾. 当read()或write()时, 读写位置会随之增加,lseek()便是用来控制该文件的读写位置. 参数fildes 为已打开的文件描述词, 参数offset 为根据参数whence来移动读写位置的位移数.

涉及到的枚举变量

  1. enum _flags
  2. {
  3. _READ = ,
  4. _WRITE = ,
  5. _UNBUF = ,
  6. _EOF = ,
  7. _ERR =
  8. };

--------------------代码实现----------------------------

The standard library function

  1. int fseek(FILE*fp,long offset,int origin)  

is identical to lseek except that fp is a file pointer instead of a file descriptor and the return value is an int status, not a position. Write fseek . Make sure that your fseek coordinates properly with the buffering done for the other functions of the library.

Here's Gregory's first solution:

  1.  
  1. /* Gregory Pietsch -- My category 0 solution to 8-4 */
  2.  
  3. int fseek(FILE *f, long offset, int whence)
  4. {
  5. if ((f->flag & _UNBUF) == && base != NULL)
      {
  6. /* deal with buffering */
  7. if (f->flag & _WRITE)
         {
  8. /* writing, so flush buffer */
  9. fflush(f); /* from 8-3 */
  10. }
         else if (f->flag & _READ)
         {
  11. /* reading, so trash buffer */
  12. f->cnt = ;
  13. f->ptr = f->base;
  14. }
  15. }
  16. return (lseek(f->fd, offset, whence) < );
  17. }

...and
here's his second, which is considerably more comprehensive:

  1.  
  1. /*
  2.  
  3. [The following solution is in the zip file as krx80401.c - RJH (ed.) ]
  4.  
  5. EXERCISE 8-4
  6.  
  7. I thought I'd improve 8-4 too. I'm trying my best to get this as close
  8. to ISO C as possible given the restrictions that I'm under. (A real
  9. implementation would have fsetpos() borrow some of the same code.)
  10.  
  11. */
  12.  
  13. /* Gregory Pietsch -- My category 0 solution to 8-4 */
  14.  
  15. #define SEEK_SET 0
  16. #define SEEK_CUR 1
  17. #define SEEK_END 2
  18.  
  19. int fseek(FILE *f, long offset, int whence)
  20. {
  21. int result;
  22.  
  23. if ((f->flag & _UNBUF) == && base != NULL) {
  24. /* deal with buffering */
  25. if (f->flag & _WRITE) {
  26. /* writing, so flush buffer */
  27. if (fflush(f))
  28. return EOF; /* from 8-3 */
  29. } else if (f->flag & _READ) {
  30. /* reading, so trash buffer --
  31. * but I have to do some housekeeping first
  32. */
  33. if (whence == SEEK_CUR) {
  34. /* fix offset so that it's from the last
  35. * character the user read (not the last
  36. * character that was actually read)
  37. */
  38. if (offset >= && offset <= f->cnt) {
  39. /* easy shortcut */
  40. f->cnt -= offset;
  41. f->ptr += offset;
  42. f->flags &= ~_EOF; /* see below */
  43. return ;
  44. } else
  45. offset -= f->cnt;
  46. }
  47. f->cnt = ;
  48. f->ptr = f->base;
  49. }
  50. }
  51. result = (lseek(f->fd, offset, whence) < );
  52. if (result == )
  53. f->flags &= ~_EOF; /* if successful, clear EOF flag */
  54. return result;
  55. }

c程序设计语言_习题8-4_重新实现c语言的库函数fseek(FILE*fp,longoffset,intorigin)的更多相关文章

  1. c程序设计语言_习题1-16_自己编写getline()函数,接收整行字符串,并完整输出

    Revise the main routine of the longest-line program so it will correctly print the length of arbitra ...

  2. c程序设计语言_习题7-6_对比两个输入文本文件_输出它们不同的第一行_并且要记录行号

    Write a program to compare two files, printing the first line where they differ. Here's Rick's solut ...

  3. c程序设计语言_习题8-6_利用malloc()函数,重新实现c语言的库函数calloc()

    The standard library function calloc(n,size) returns a pointer to n objects of size size , with the ...

  4. c程序设计语言_习题1-19_编写函数reverse(s)将字符串s中字符顺序颠倒过来。

    Write a function reverse(s) that reverses the character string s . Use it to write a program that re ...

  5. c程序设计语言_习题1-18_删除输入流中每一行末尾的空格和制表符,并删除完全是空格的行

    Write a program to remove all trailing blanks and tabs from each line of input, and to delete entire ...

  6. c程序设计语言_习题1-13_统计输入中单词的长度,并且根据不同长度出现的次数绘制相应的直方图

    Write a program to print a histogram of the lengths of words in its input. It is easy to draw the hi ...

  7. c程序设计语言_习题1-11_学习单元测试,自己生成测试输入文件

    How would you test the word count program? What kinds of input are most likely to uncover bugs if th ...

  8. c程序设计语言_习题1-9_将输入流复制到输出流,并将多个空格过滤成一个空格

    Write a program to copy its input to its output, replacing each string of one or more blanks by a si ...

  9. 3.1 C语言_实现AVL平衡二叉树

    [序] 上节我们实现了数据结构中最简单的Vector,那么来到第三章,我们需要实现一个Set set的特点是 内部有序且有唯一元素值:同时各种操作的期望操作时间复杂度在O(n·logn): 那么标准的 ...

随机推荐

  1. 《C和指针》

    <C和指针> static global varibale VS global variable static function VS normal function 数组变量 VS 字符 ...

  2. Wix: Show conditional message box

    For example: Scenario: Show message box only during installation and MYPROPERTY has to be equal to & ...

  3. update语句

    [update cicm.cicmodt0702 set msgbody = :1 where msgid between :2 and :3         ] [update cicm.cicmo ...

  4. [翻译][MVC 5 + EF 6] 12[完结]:高级场景

    原文:Advanced Entity Framework 6 Scenarios for an MVC 5 Web Application 1.执行原生SQL查询: EF Code First API ...

  5. shell环境

    1 引言 一个进程运行在shell环境中,理解进程运行的环境是十分重要的.环境影响着进程的行为,利用环境提供的便利,可以极大地提高开发效率.本节深入讨论shell中与进程有关的环境问题,包括命令行参数 ...

  6. C# WinForm实现控件拖动实例介绍

    主要是设计控件的MouseDown.MouseLeave.MouseMove事件.一步步来吧:1.定义一个枚举类型,描述光标状态 private enum EnumMousePointPosition ...

  7. MIT 2012分布式课程基础源码解析-线程池实现

    主要内容 ScopedLock 队列实现 线程池实现 在正式讲解线程池实现之前,先讲解两个有用的工具类: ScopedLock fifo队列 ScopedLock: ScopedLock是局域锁的实现 ...

  8. VirtualBox 安装虚拟机时出现错误 VT-x features locked or unavailable in MSR.

    修改安装好的虚拟机的cup的个数重新启动的时候报了上述错误. 参考博文:http://blog.csdn.net/zklth/article/details/7019990 错误:VT-x featu ...

  9. WPF一个简单的垂直菜单样式的实现

    以前制作类似于垂直菜单功能的控件我都是Listbox和一个Popup实现的,今天尝试着用Menu做了一个简单垂直菜单,就当是做了个小练习写了这篇随笔~: 有什么不对的地方希望大家指正,分享和记录也是一 ...

  10. Linux统计某文件夹下文件、文件夹的个数

    统计某文件夹下文件的个数 ls -l |grep "^-"|wc -l 统计某文件夹下目录的个数 ls -l |grep "^d"|wc -l 统计文件夹下文件 ...