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

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

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

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

对于文本流,offset心须为0,或者是由函数ftell()返回的值(此时origin的值必须是SEEK_SET)。

 

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

 

The standard library function

1
int fseek(FILE*fp,longoffset,intorigin)  

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:

 
c程序设计语言_习题8-4_重新实现c语言的库函数fseek(FILE*fp,longoffset,intorigin)
/* Gregory Pietsch -- My category 0 solution to 8-4 */
 
int fseek(FILE *f, long offset, int whence)
{
    if ((f->flag & _UNBUF) == 0 && base != NULL) {
        /* deal with buffering */
        if (f->flag & _WRITE) {
            /* writing, so flush buffer */
            fflush(f);  /* from 8-3 */
        } else if (f->flag & _READ) {
            /* reading, so trash buffer */
            f->cnt = 0;
            f->ptr = f->base;
        }
    }
    return (lseek(f->fd, offset, whence) < 0);
}
c程序设计语言_习题8-4_重新实现c语言的库函数fseek(FILE*fp,longoffset,intorigin)

 



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

 
c程序设计语言_习题8-4_重新实现c语言的库函数fseek(FILE*fp,longoffset,intorigin)
/*
 
[The following solution is in the zip file as krx80401.c - RJH (ed.) ]
 
EXERCISE 8-4
 
I thought I‘d improve 8-4 too.  I‘m trying my best to get this as close
to ISO C as possible given the restrictions that I‘m under.  (A real
implementation would have fsetpos() borrow some of the same code.)
 
*/
 
/* Gregory Pietsch -- My category 0 solution to 8-4 */
 
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
 
int fseek(FILE *f, long offset, int whence)
{
    int result;
 
    if ((f->flag & _UNBUF) == 0 && base != NULL) {
        /* deal with buffering */
        if (f->flag & _WRITE) {
            /* writing, so flush buffer */
            if (fflush(f))
                return EOF;  /* from 8-3 */
        } else if (f->flag & _READ) {
            /* reading, so trash buffer --
             * but I have to do some housekeeping first
             */
            if (whence == SEEK_CUR) {
                /* fix offset so that it‘s from the last 
                 * character the user read (not the last
                 * character that was actually read)
                 */
                if (offset >= 0 && offset <= f->cnt) {
                    /* easy shortcut */
                    f->cnt -= offset;
                    f->ptr += offset;
                    f->flags &= ~_EOF; /* see below */
                    return 0;
                } else
                    offset -= f->cnt;
            }
            f->cnt = 0;
            f->ptr = f->base;
        }
    }
    result = (lseek(f->fd, offset, whence) < 0);
    if (result == 0)
        f->flags &= ~_EOF; /* if successful, clear EOF flag */
    return result;
}
c程序设计语言_习题8-4_重新实现c语言的库函数fseek(FILE*fp,longoffset,intorigin)

 

c程序设计语言_习题8-4_重新实现c语言的库函数fseek(FILE*fp,longoffset,intorigin),布布扣,bubuko.com

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

上一篇:Java中内存泄露及垃圾回收机制


下一篇:C++ 文件IO