Unix standard I/O summary and comparison to file I/O

Source: Internet
Author: User

We can treat file I/O as a system call and the kernel to perform I/O operations, where the concept of page caching (cache area) is involved, and file I/O execution is related to the cache.

While standard I/O is the encapsulation of system I/O calls, standard I/O also has the concept of buffers and row caches. It is because of this two level cache mode. Results in low standard I/O efficiency.

When a stream is opened, the standard I/O function fopen Returns a pointer to the file object. This object is typically a structure that contains all the information that the standard I/O library needs to manage the flow, including the file descriptor for the actual I/O, the pointer to the buffer, the length of the buffer, the number of characters currently in the buffer, and the error flag. To reference a stream, you pass the file pointer as a parameter to each standard I/O function.


For standard input, standard output, and standard errors, their file descriptors correspond to Stfin_fileno, Stdout_fileno, and Stderr_fileno. These three standard I/O flows are referenced by predefined stdin, stdout, and stderr. These three file pointers and standard I/O functions are defined in the header file <stdio.h>.

Buffer

The standard I/O library provides buffering to minimize the use of read and write calls. Three types of buffering are available:

1) Full buffering: actual I/O operations are required after filling the standard I/O buffers.

2) Row buffering: The standard I/O library performs I/O operations when a newline character is encountered in input and output.

3) without buffering: The standard I/O library does not buffer characters for storage.

In general, standard error is not buffered, the flow of the open end device is buffered, all other flows are fully buffered. When the stream is fully buffered, but the buffer is partially filled out, the fflush function can be flushed.

You can call the following function to change the buffer type:

#include <stdio.h>

void Setbuf (FILE *stream, char *buf);

int setvbuf (FILE *stream, Char *buf, Intmode, size_t size);

At any time, we can force flushing a stream:

#include <stdio.h>

int fflush (FILE *stream);

This function will cause all the unread data of the stream to be transferred to the kernel. As a special case, if the FP is null, this function causes all output streams to be flushed.

Open stream


#include <stdio.h>

FILE *fopen (const char *path, const char*mode);

FILE *fdopen (int fd, const char *mode);

File *freopen (const char *path, const char*mode, file *stream);

The differences between the three functions are:

1) fopen open a specified file.

2) Fropen open a specified file on a specified leave, and if the stream is already open, close the stream first. If the stream is already directed, fopen clears the orientation. This function is typically used to open a file that has been specified as a pre-defined stream: standard input, standard output, or standard error.

3) Fdopen Gets an existing file descriptor and combines a standard I/O stream with the descriptor. This function is commonly used for descriptors that are returned by creating pipelines and network communication functions. Because these special types of files cannot be opened with the standard I/ofopen function, all we must first call the device-specific function to obtain a file descriptor, and then use fopen to associate a standard I/O with that description typeface.

The mode parameter can be used with the following 15 different values:
R or RB: open for Read

W or WB: Truncate the file to 0 long, or create it for writing

A or AB: add; open for write in file, or open for write

r+ or R+b or rb+: Open for Read and write

w+ or W+b or wb+: Truncate the file to 0, or open for read and write

A + or a+b or ab+: Open or create for read and write at end of file

#include <stdio.h>

int fclose (FILE *fp);

Flushes the output data in the buffer before the file is closed. If the standard I/O library has automatically allocated a buffer for the stream, the buffer is freed.

Read and Write Streams

Once the stream is turned on, you can select from three different types of unformatted I/O to read and write to:

1) one character at a time is I/O. Read or write one character at a time, and if the stream is a buffer, the standard I/O function handles all buffers.

2) I/O for each line. If you want to read or write one line at a time, use Fgets and fputs. Each line terminates with a newline character. When calling fgets, you should indicate the maximum number of presidents that can be processed.

3) Direct I/O. The fread and Fwrite functions support this type of I/O.

One character I/O per
Input function:

#include <stdio.h>

int getc (FILE *stream);

int fgetc (FILE *stream);

int GetChar (void);

GetChar () is equivalent to GETC (stdin). The difference between getc and fgetc is that GETC can be implemented as a macro, while fgetc cannot be implemented as a macro.

All three functions return the same value, either in error or at the end of the file. In order to differentiate between errors and reach the end of a file, you must call the Ferror and feof functions.

#include <stdio.h>

int feof (FILE *stream);

int ferror (FILE *stream);

The return value of these two functions: Returns a value other than 0 if the condition is true, otherwise returns 0.

Each stream maintains two flags in the file object: Error flag and file end flag

Calling Clearerr clears both flags.

void Clearerr (FILE *stream);

After reading the data from the stream, you can call ungetc to press the character back into the reflow.

int ungetc (int c, FILE *stream);

The characters pressed into the reflow can then be read out of the stream, but the order of the characters read out is the opposite of the order in which they are pressed back.

For output functions:

#include <stdio.h>

int PUTC (int c, FILE *stream);

int FPUTC (int c, FILE *stream);

int Putchar (int c);

As with the input function, Putchar (c) is equivalent to PUTC (c, stdout). PUTC can be implemented as a macro.

I/O per line
#include <stdio.h>

Char *fgets (char *s, int size, file*stream);

Char *gets (char *s);

Fgets reads from the specified stream and must specify a buffer length of size. This function reads until the next newline character, but not more than n-1 characters, and the read-in character is fed into a buffer. The buffer ends with a null character. If the number of characters for a row (including the last newline character) exceeds n-1, Fgets returns only an incomplete row, but the buffer always ends with a null character. The next call to Fgets will continue to read the row.

Gets read from standard input. It is an deprecated function because the buffer length cannot be specified, which can cause a buffer overflow to be written into the storage space after the buffer, resulting in unpredictable consequences.

Fputs and puts provide the ability to output one line at a time.

int fputs (const char *s, FILE *stream);

int puts (const char *s);

Binary I/O

#include <stdio.h>

size_t fread (void *ptr, size_t size,size_t nmemb, FILE *stream);

size_t fwrite (const void *ptr, Size_tsize, size_t nmemb, FILE *stream);

The above two functions can read or write the entire structure at once.

Locating a Stream


There are three ways to locate standard I/O streams

1) Ftell and fseek. These two functions require the location of the file to be stored in a long shape.

2) Ftello and Fseeko. They can make file file offsets not necessarily using long shaping. They used the off_t data type instead of long shaping.

3) Fgetpos and Fsetpos. They use the abstract data type fpos_t to record the location of the file. This data exhaustion can be defined as the length required to record the location of a file.

#include <stdio.h>

int fseek (FILE *stream, long offset, intwhence);

Long Ftell (FILE *stream);

int Fseeko (FILE *stream, off_t offset, intwhence);

off_t Ftello (FILE *stream);

int Fgetpos (FILE *stream, fpos_t *pos);

int Fsetpos (FILE *stream, fpos_t *pos);

Format I/O

Output:


#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, Constchar *format, ...);


Input:


#include <stdio.h>


int scanf (const char *format, ...);


int fscanf (FILE *stream, const Char*format, ...);


int sscanf (const char *STR, const Char*format, ...);


Fileno function


The standard I/O library eventually calls the I/O system call function. Each standard I/O stream has a file descriptor associated with it, and you can call the Fileno function on a stream to get its descriptor.


int Fileno (FILE *stream);


Temporary files


#include <stdio.h>


Char *tmpnam (char *s);


FILE *tmpfile (void);


The Tmpnam function produces a valid pathname string that differs from the existing file name. Each time it is called, it produces a different pathname, which is called tmp_max times.


Tmpfile Create a temporary binary file.


In addition, there are two similar functions:


#include <stdio.h>


Char *tempnam (const char *dir, const CHAR*PFX);


int mkstemp (char *template);

Unix standard I/O summary and comparison to file I/O

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.