Advanced Programming in the UNIX Environment: Second Edition [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Advanced Programming in the UNIX Environment: Second Edition [Electronic resources] - نسخه متنی

W. Richard Stevens; Stephen A. Rago

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید



5.10. Positioning a Stream


There are three ways to position a standard I/O stream:

  • The two functions ftell and fseek. They have been around since Version 7, but they assume that a file's position can be stored in a long integer.

  • The two functions ftello and fseeko. They were introduced in the Single UNIX Specification to allow for file offsets that might not fit in a long integer. They replace the long integer with the off_t data type.

  • The two functions fgetpos and fsetpos. They were introduced by ISO C. They use an abstract data type, fpos_t, that records a file's position. This data type can be made as big as necessary to record a file's position.

  • Portable applications that need to move to non-UNIX systems should use fgetpos and fsetpos.

    #include <stdio.h>
    long ftell(FILE *

    fp );

    Returns: current file position indicator if OK, 1L on error

    int fseek(FILE *

    fp , long

    offset , int

    whence );

    Returns: 0 if OK, nonzero on error

    void rewind(FILE *

    fp );

    Section 3.6: SEEK_SET means from the beginning of the file, SEEK_CUR means from the current file position, and SEEK_END means from the end of file. ISO C doesn't require an implementation to support the SEEK_END specification for a binary file, as some systems require a binary file to be padded at the end with zeros to make the file size a multiple of some magic number. Under the UNIX System, however, SEEK_END is supported for binary files.

    For text files, the file's current position may not be measurable as a simple byte offset. Again, this is mainly under non-UNIX systems that might store text files in a different format. To position a text file,

    whence has to be SEEK_SET, and only two values for

    offset are allowed: 0meaning rewind the file to its beginningor a value that was returned by ftell for that file. A stream can also be set to the beginning of the file with the rewind function.

    The ftello function is the same as ftell, and the fseeko function is the same as fseek, except that the type of the offset is off_t instead of long.

    #include <stdio.h>
    off_t ftello(FILE *

    fp );

    Returns: current file position indicator if OK, (off_t)1 on error

    int fseeko(FILE *

    fp , off_t

    offset , int

    whence );

    Returns: 0 if OK, nonzero on error

    Recall the discussion of the off_t data type in Section 3.6. Implementations can define the off_t type to be larger than 32 bits.

    As we mentioned, the fgetpos and fsetpos functions were introduced by the ISO C standard.

    #include <stdio.h>
    int fgetpos(FILE *restrict

    fp , fpos_t *restrict

    pos );
    int fsetpos(FILE *

    fp , const fpos_t *

    pos );

    Both return: 0 if OK, nonzero on error

    The fgetpos function stores the current value of the file's position indicator in the object pointed to by

    pos . This value can be used in a later call to fsetpos to reposition the stream to that location.


      / 369