Standard IO library for UNIX environments

Source: Internet
Author: User

UNIX file io is for file descriptors, while standard IO operations revolve around streams. When a stream is initially created, it is not directed. If a multibyte IO function is used on an non-directed stream, the orientation of the stream is set to wide orientation, and if a single byte IO function is used on the non-directed stream, the orientation of the stream is set to byte-oriented.
Stdio.h defines three standard streams, stdin, stdout, and stderr, which are standard input, standard output, and standard error.

Buffering--

The standard IO Library provides buffering to minimize the number of read and write calls that are used. It also automatically buffers each IO stream, avoiding the hassle of applications needing to consider this. Standard IO provides three types of buffering:
1, full buffer. In this case, the actual IO operation is not performed until the standard IO buffer is filled.
2, row buffer. In this case, the standard IO library performs IO operations when a newline character is encountered in the input and output.
3, without buffering. The standard IO library does not buffer characters for storage. Standard error stream stderr are usually unbuffered, which allows error messages to be displayed as quickly as possible, regardless of whether they contain a newline character.
Call the following function to change the buffer type:

#include <stdio.h>voidchar *buf);intcharint mode, size_t size);

Call the Fflush function to force flushing a stream:

#include <stdio.h>int fflush(FILE *stream);

When a process terminates normally, call the Exit function directly, or return from the main function, all standard IO streams with write buffered data will be flushed and all open standard IO streams will be closed.

Flow Operation--

The following three functions can open a standard IO stream:

#include <stdio.h>FILE* fopen(constcharconstchar *mode);FILE* fdopen(intconstchar *mode);FILE* freopen(constcharconstchar *mode, FILE *stream);

Streams can be divided into non-formatted IO and formatted IO, which are also divided into character io, line io, and binary IO, the latter such as common prntf and scanf.
The Fclose function closes an open stream:

#include <stdio.h>int fclose(FILE *fp);

The Fwide function can set the direction of the flow:

#include <wchar.h>intint mode);

Functions related to errors or to the end of the file:

#include <stdio.h>int*stream);int*stream*stream);
Flow Positioning-
#include <stdio.h>int*stream);int*streamint*stream);int*stream*pos);int*stream*pos*stream);int*streamint whence);
Character io--
#include <stdio.h>intgetc*stream);int*stream);int getchar(void);int ungetc(int*stream);int putc(int*stream);int fputc(int*stream);int putchar(int c);
Line io--
#include <stdio.h>char* gets(char *s);char* fgets(charint size, FILE *stream);intputs(constchar *s);intfputs(constchar *s, FILE *stream);
Binary io--
#include <stdio.h>size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);size_t fwrite(constvoid *ptr, size_t size, size_t nmemb, FILE *stream);
Common Format io--
#include <stdio.h>int printf(Const Char*format, ...);int fprintf(FILE *stream,Const Char*format, ...);int sprintf(Char*STR,Const Char*format, ...);int snprintf(Char*STR, size_t size,Const Char*format, ...);#include <stdarg.h>int vprintf(Const Char*format, va_list AP);int vfprintf(FILE *stream,Const Char*format, va_list AP);int vsprintf(Char*STR,Const Char*format, va_list AP);intvsnprintf (Char*STR, size_t size,Const Char*format, va_list AP);#include <stdio.h>int scanf(Const Char*format, ...);int fscanf(FILE *stream,Const Char*format, ...);int sscanf(Const Char*STR,Const Char*format, ...);#include <stdarg.h>intVSCANF (Const Char*format, va_list AP);intVSSCANF (Const Char*STR,Const Char*format, va_list AP);intVFSCANF (FILE *stream,Const Char*format, va_list AP);
Insufficient standard IO-

One disadvantage of the standard IO Library is its inefficiency, which is related to the amount of data it needs to replicate. When using fgets and fputs each line of functions, it is usually necessary to replicate two data: once between the kernel and the standard IO buffer (when read and write are called), the second time between the standard IO buffers and the row buffers in the user program. The Fast IO library avoids this by making the function that reads a line return a pointer to that row instead of copying the row to another buffer.

Standard IO library for UNIX environments

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.