Embedded Linux system Programming (III)--Standard IO Library

Source: Internet
Author: User
Tags fread rewind

embedded Linux system Programming (III)--standard IO Library

     with file io standard fopen fclose fread , fwrite io function, these functions are invoked on their own linux file io

First, Standard buffering mechanism of IO library functions

io device access speed with cpu io device with cpu speed mismatch for block device

write back to the disk and the data will be written to the page cache only. In the case of a deferred write mechanism, the operating system periodically brushes the data that is placed in the page cache to disk. Unlike the asynchronous write mechanism, the deferred write mechanism does not notify the application when the data is fully written to disk, and the asynchronous write mechanism is returned to the application when the data is fully written to disk. So the delay write mechanism of the province is the risk of data loss, and the asynchronous write mechanism will not have this concern.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/83/C8/wKiom1d8c3LiyRRUAADWROzvIEk336.png "title=" Picture 1.png "alt=" Wkiom1d8c3liyrruaadwrozviek336.png "/>

from the top Table can know when the buffer reaches 4 096 size, continuing to increase the buffer size has little impact on IO performance. This 4096 size is determined by the file system block size, because the file system used for the above test is a Linux ext2, the block size is 4096 .

Standard io Library is a good solution to set the buffer size problem, standard io will choose the best cache size, so we no longer care about setting the size of the cache, in fact, the standard io the library will be for each IO The flow is automatically buffered and avoids the hassle of the application needing to consider this.

Standard IO provides three types of buffering mechanisms:

Full buffer:

fill the standard io buffers before the actual io operation. For files on disk, it is usually fully buffered by the standard IO Library. When performing the first I/O operation on a stream , the relevant standard I/O functions typically call malloc to get the buffers that need to be used.

Row buffers:

When a line break is encountered in input and output, the standard Io Library performs io operations. The use of a row buffer typically involves a terminal (for example, standard input and standard output).

There are two restrictions for row buffering. First, because the standardIO io operation. Second, any time just pass the standard io library requirements from ( Span style= "FONT-FAMILY:CALIBRI;" >a b ) a row-cached stream (which requires data from the kernel) to get input data will cause flushing of all rows buffered output streams (since the data that is read later may be the data that was previously output). In fact the second case we will often encounter when we first call printf When you output a string of characters without line breaks, execute this printf The statement does not immediately display the data we output in the screen, when we next call scanf when we read data from standard input, we see the data from the previous output.

Without buffering:

Standard the IO Library does not store characters. For example, if you write a fputs to a stream without buffering using the standard IO function , the function is likely to call directly Write The system call writes these characters immediately to the relevant file. The standard error stream stderr is not buffered, so that the error information can be displayed as soon as possible.


Second, standard IO library functions

#include <stdio.h>

FILE *fopen (const char *path, const char *mode);//mode for Operation permissions

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

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

Successfully returns a file stream pointer, fails to return NULL, and sets the errno global variable.

Mode:

R: read-only open file, return file stream pointer at start of file

r+: Read-write mode open file, return file stream pointer at start of file

W: If file exists, clear file write, if file does not exist, create file write

w+: Read and write mode open file, file does not exist create file, empty file exists

A: append write to open file, create file if not present, stream pointer at end of file

A +: Read and append write open, if the file does not exist, create a file,

#include <stdio.h>

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

PTR: The location of the read data store

Size: sizes of each cell read

NMEMB: Number of units read

Stream: Read data from which file stream

The number of bytes read was successfully returned, the end of the file returned 0, and a short number was returned.

Fread cannot differentiate between reaching the end of a file and errors, so you must use the feof and ferror functions to determine whether to reach the end of the file or an error occurred.

size_t fwrite (const void *ptr, size_t size, size_t nmemb,file *stream);

PTR: The location of the data being written

Size: Each time a unit is written

NMEMB: Number of units written

Stream: Which file stream to write to

The number of bytes written successfully returned to the end of the file, returning 0, and a short number of failures.

#include <stdio.h>

int fclose (FILE *fp);

Successfully returns 0, refreshes the cache, fails to return EOF, and sets errno.

#include <stdio.h>

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

Long Ftell (FILE *stream);

void Rewind (FILE *stream);

Rewind () function has no return value,fseek successfully returned 0,ftell successfully returned the current offset, failed to return -1 , and set errno .



Reference blog:

Linux Standard IO cache (Blog Park is about thinking )

Linux Manual

This article from "Endless life, Struggle not only" blog, reprint please contact the author!

Embedded Linux system Programming (III)--Standard IO Library

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.