The difference between standard io and file io "Go"

Source: Internet
Author: User

This article was reproduced from:

Let's start by knowing what the standard IO and file io are.

Standard IO: Standard I/O is a standard I/O model established by ANSI C and is defined in a standard function package and stdio.h header file, with some portability. The standard IO Library handles many details. such as cache allocation, to optimize the length of the execution IO. Standard IO provides three types of caching.

(1) Full cache: The actual IO operation is not performed until the standard IO cache is filled.
(2) Row cache: The standard IO library performs IO operations when new line characters are encountered in input or output.
(3) without cache: stderr.

File io: File io is called IO without cache (unbuffered I/O). Without caching, each read,write is called by a system call in the kernel. This is generally referred to as the basic IO service provided by the low-level i/o--operating system, tied to the OS, specific to the Linix or UNIX platform.

2 differences

First: A significant difference between the two is that the standard I/O defaults to a buffering mechanism, such as calling the Fopen function, not only opening a file, but also creating a buffer (two buffers will be created in read-write mode), and a data structure that contains data about the file and buffer. Low-level I/O is generally not buffered, you need to create buffers yourself, but in fact, in Linix or UNIX systems, there is a technique called kernel buffering to improve efficiency, and read-write calls are data replication between kernel buffers and process buffers.

Secondly, from the operation of the device to distinguish, file I/O mainly for file operations, read and write hard disk, it is the file descriptor, the standard I/O for the console, print output to the screen, etc., it operates a character stream. For different device features, different API access must be the most efficient.

Finally look at the functions they use

Standard IO

File IO (low-level IO)

Open it



Shut down









1.fopen and Open

Standard I/O uses the fopen function to open a file:

file* fp=fopen (const char* path,const char *mod)

Where path is the file name, mod is used to specify a string for the open mode of the files, such as "R", "W", "w+", "a" and so on, can be added with the letter B to be opened in binary mode (for *nix system, there is only one file type, so there is no difference), if opened successfully, Returns a file file pointer, if the failure returns NULL, here the pointer is not a pointer to the actual file, but a packet of information about the file, including the buffer information used by the file.

File IO uses the Open function for opening a file:

int Fd=open (char *name,int how);

Similar to fopen, name represents the file name string, and how specifies the open mode: o_rdonly (read-only), O_wronly (write-only), O_RDWR (readable writable), and other modes please man 2 open. The successful return of a positive integer is called a file descriptor, which differs significantly from standard I/O, and returns 1 if it fails, and is different from standard I/O return null.

2.fclose and Close

In contrast to open files, standard I/O uses fclose to close the file, pass the file pointer in, and, if successful, return 0, or EOF
Like what:

if (fclose (FP)!=0)
printf ("Error in closing file");

and file IO uses close to close open files, similar to fclose, except that when an error occurs, the return is 1 instead of EOF, and the successful shutdown also returns 0. The C language uses error code as a traditional way of handling errors.

3. Reading files ,getc,fscanf,fgets and read

File reads in standard I/O can be read by using GETC, a character of one character, can also be read using the Get (read standard IO), fgets in string units (read to the back of the first newline character encountered), gets (takes a parameter, The file pointer) does not determine whether the target array can accommodate the characters that are read in, may cause storage overflow (not recommended), and Fgets uses three parameters:
char * fgets (char *s, int size, FILE *stream);

The first parameter, like the GET, is used to store the input address, the second argument is an integer, the maximum length of the input string, and the last parameter is the file pointer, pointing to the file to be read. Finally, fscanf, similar to scanf, only adds a parameter for specifying the action file, such as FSCANF (FP, "%s", words)
The read function is used in file IO to read the file opened by the Open function, and the function prototype is as follows:

ssize_t numread=read (int fd,void *buf,size_t qty);

Where FD is the file descriptor returned by open, BUF is used to store the destination buffer for the data, and qty Specifies the number of bytes to read. If read successfully, returns the number of bytes read (less than or equal to qty)

4. Determine the end of the file

If the attempt to read reaches the end of the file, the standard IO getc returns the special value EOF, while fgets encounters EOF returns NULL, and for *nix's read function, the situation is different. Read reads the number of bytes specified by the Qty, and the final reading may not be as much as you require (qty), and the Read function returns 0 when read to the end.

5. Writing files :putc,fputs,fprintf and write

In correspondence with the read file, standard C-language I/O uses PUTC to write characters, such as:


The first argument is a character, and the second is a file pointer. And fputs is similar to this:

Fputs (BUF,FP);

Only the first parameter is replaced with a string address. fprintf, like printf, adds a parameter to specify which files to write, such as:

fprintf (stdout, "Hello%s.\n", "Dennis");

Remember that fscanf and fprintf use the file pointer as the first argument, and putc,fputs as the second argument.

The Write function is provided in file IO for writing to the file, and the prototype is similar to read:

ssize_t result=write (int fd,void *buf, size_t amt);

FD is the file descriptor, BUF is the memory data that will be written, and the number of bytes that Amt is to write. If the write successfully returns the number of bytes written, the comparison between result and AMT can determine if the write is normal, and if the write fails return 1

6. Random Access :fseek (), Ftell (), and Lseek ()

Standard I/O uses fseek and ftell for random file access, first look at the Fseek function prototype

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

The first parameter is the file pointer, and the second parameter is a long offset (offset) that represents the distance from the starting point to move. The third parameter is the pattern used to specify the starting point, and stdio.h specifies the following pattern constants:

Seek_set file at the beginning
Seek_cur Current Location
At the end of the Seek_end file

See a few invocation examples:
Fseek (Fp,0l,seek_set); Locate the beginning of the file
Fseek (Fp,0l,seek_end); Navigate to end of file
Fseek (fp,2l,seek_cur); The current position of the file moves forward by 2 bytes

The Ftell function is used to return the current position of the file, and the return type is a long type, such as the following call:

Fseek (fp,0l,seek_end);//position to end
Long Last=ftell (FP); Return to current position

Then the last is the number of bytes of the file that the file pointer fp points to.

Similar to standard I/O, the *nix system provides lseek to complete the functions of fseek, with the following prototypes:

off_t lseek (int fildes, off_t offset, int whence);

Fildes is the file descriptor, and offset is also the offset, whence is also the specified starting point pattern, the only difference is that the Lseek has a return value, if successful returns the position before the pointer changes, otherwise returns-1. The whence value is the same as the fseek: Seek_set,seek_cur,seek_end, but can also be replaced with an integer 0,1,2.

The difference between standard io and file io "Go"

Related Article

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: 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.