Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

input output and serialization — concept C23

Standard I/O uses `FILE` objects with an open mode, position, and error/end indicators; the number of successfully processed items is part of the result. Text and binary serialization require size, encoding, byte-order, and short-read/write checks; external data must never become a format string.

Version: C23

Syntax

input output and serialization

Standard I/O uses FILE objects with an open mode, position, and error/end indicators; the number of successfully processed items is part of the result. Text and binary serialization require size, encoding, byte-order, and short-read/write checks; external data must never become a format string.

For text serialization, first use snprintf with the buffer capacity and check whether its result is negative or not less than that capacity. For files, check every fread/fwrite result and define the format with explicit-width fields instead of writing a raw struct.

An empty file, short read, failure after a partial write, and data containing a \0 byte require separate paths. Text mode may translate line endings, a stream position is not always a byte count, and buffered output may require fflush or a successful fclose.

Examples

#include <stdio.h>

int main(void) {
    char buffer[16];
    int length = snprintf(buffer, sizeof buffer, "%s:%d", "item", 7);
    printf("%d %s\n", length, buffer);
    return 0;
}

Result

6 item:7

Standard I/O uses `FILE` objects with an open mode, position, and error/end indicators; the number of successfully processed items is part of the result. Text and binary serialization require size, encoding, byte-order, and short-read/write checks; external data must never become a format string.

Sources