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.9. Binary I/O


The functions from Section 5.6 operated with one character at a time, and the functions from Section 5.7 operated with one line at a time. If we're doing binary I/O, we often would like to read or write an entire structure at a time. To do this using getc or putc, we have to loop through the entire structure, one byte at a time, reading or writing each byte. We can't use the line-at-a-time functions, since fputs stops writing when it hits a null byte, and there might be null bytes within the structure. Similarly, fgets won't work right on input if any of the data bytes are nulls or newlines. Therefore, the following two functions are provided for binary I/O.


[View full width]

#include <stdio.h>
size_t fread(void *restrict

ptr , size_t

size ,
size_t

nobj ,
FILE *restrict

fp );
size_t fwrite(const void *restrict

ptr , size_t

size , size_t

nobj ,
FILE *restrict

fp );

Both return: number of objects read or written

These functions have two common uses:

  • Read or write a binary array. For example, to write elements 2 through 5 of a floating-point array, we could write

    float data[10];
    if (fwrite(&data[2], sizeof(float), 4, fp) != 4)
    err_sys("fwrite error");

    Here, we specify

    size as the size of each element of the array and

    nobj as the number of elements.

  • Read or write a structure. For example, we could write

    struct {
    short count;
    long total;
    char name[NAMESIZE];
    } item;
    if (fwrite(&item, sizeof(item), 1, fp) != 1)
    err_sys("fwrite error");

    Here, we specify

    size as the size of structure and

    nobj as one (the number of objects to write).

  • The obvious generalization of these two cases is to read or write an array of structures. To do this,

    size would be the sizeof the structure, and

    nobj would be the number of elements in the array.

    Both fread and fwrite return the number of objects read or written. For the read case, this number can be less than

    nobj if an error occurs or if the end of file is encountered. In this case ferror or feof must be called. For the write case, if the return value is less than the requested

    nobj , an error has occurred.

    A fundamental problem with binary I/O is that it can be used to read only data that has been written on the same system. This was OK many years ago, when all the UNIX systems were PDP-11s, but the norm today is to have heterogeneous systems connected together with networks. It is common to want to write data on one system and process it on another. These two functions won't work, for two reasons.

  • Chapter 16. The real solution for exchanging binary data among different systems is to use a higher-level protocol. Refer to Section 8.2 of Rago [1993] or Section 5.18 of Stevens, Fenner, & Rudoff [2004] for a description of some techniques various network protocols use to exchange binary data.

    We'll return to the fread function in Section 8.14 when we'll use it to read a binary structure, the UNIX process accounting records.


    • / 369