A detailed description of Linux standard I/O functions

Source: Internet
Author: User
Tags file copy fread

1, I/O operation is the basis of the system.

The I/O representation of input "inputs" and output "outputs". I/O operations are the basis of system implementation. If there is no I/O operation, so some system files will not be stored, much less processing and analysis, the results of the system operation is not seen by the user.


2. The difference between system IO and standard IO

I/O is divided into standard IO and system IO. Standard IO is called stdio, and system IO is also known as file IO. System IO is the interface that the kernel provides to the user to handle IO operations. For example, standard C is not able to handle input and output problems. The input and output processing of the program must be implemented with the help of the interface provided by the kernel. Standard I/O is a two-time package based on system IO. To shield the differences between different architectures and increase the portability of the program.


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7C/9B/wKiom1bTnWTw3K8YAAA0LI_W5Bo800.png "title=" Qq20160229092157.png "alt=" Wkiom1btnwtw3k8yaaa0li_w5bo800.png "/>


As you can see, standard I/O is implemented based on system IO. Fopen is implemented based on the Open method, and Fclose is implemented based on close. Fgetc,fputc,fgets,fputs,fread,fwrite, which is based on the read and write methods, is implemented, and Fseek,ftell,rewind is based on the Lseek method.

Throughout the entire process of standard IO, there is an important structure: file, which runs through the whole process of the system IO, has an important shaping variable: int fd (where FD means: file descriptot, filename descriptor). What is the relationship between the two?

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7C/9C/wKiom1bTpKnj48M0AAEWEErMNlM098.png "title=" Qq20160229095317.png "width=" "height=" 452 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:700PX;HEIGHT:452PX; "alt = "Wkiom1btpknj48m0aaeweermnlm098.png"/>


3. Use of standard I/O related functions:

3.1 fopen function:

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

Parameter: const char *path opens the file under the specified path. The Const modifier in order to enhance the program security "parameter cannot be modified" const char *mode indicates how the file is opened:

Mode Meaning
R Read-only open, file location pointer at the beginning of file "file must exist beforehand, otherwise error"
r+ Read-only mode open, file location pointer at the beginning of file "file must exist beforehand, otherwise error"
W There is empty, none is created. The file location pointer is at the beginning of the file
w+ There is empty, none is created. The file location pointer is at the beginning of the file
A Open in an additional way, the file position pointer is not visible at the end of the
A + Opened in a read or attached way, the file location pointer depends on how the user operates. The reading is at the beginning, and the attachment is at the end.

Return value: Open succeeds returns the file type pointer, failure returns a null pointer, and writes the errno value.

Examples of usage:

     #include  <stdio.h>         #include  <stdlib.h>     #include  <errno.h>     #include  <string.h>    int main  ()     {     file *f = null;    f = fopen ("tmp", "W");     if (f == null)     {    //fprintf (stderr, "fopen ()   Failed ! errno = %d\n ", errno);     //view the cause of the error. Go:/usr/include/asm-generic/errno-bash.h    //Use the Perro function     //perror (" fopen () ");     //another useful function     fprintf (stderr," fopen ():%s\n ", Strerror ( errno));     exit (1);     }    puts ("OK");     fclose (f);     //exit (0);     } 


3.2 Fclose:

Function: Closes the open file pointer and refreshes the cache.

Usage: int fclose (FILE *fp);

Parameters: File *FP open files pointer.

Return value: Successfully returns 0 if EOF is not successfully returned, and the errno is set.


3.3, Fgetc FPUTC

Function:

FGETC: Reads the next character from the stream, "characters read in to reshape".

FPUTC: Output one character

Usage:

int fgetc (FILE *stream);

int FPUTC (int c, FILE *stream);

return value:

Fgetc (), getc () and GetChar () return the character read as an unsigned char cast to an

int or EOF on end of file or error.

FPUTC (), PUTC () and Putchar () return the character written as an unsigned char cast to

An int. or EOF on Error

Note: The return value of FGETC is to be received with an int and cannot be defined as a char type.

EOF is the abbreviation for End of File. In the C language, it is a macro defined in the standard library. Most people think that there is an EOF in the file that represents the end of the file. But this view is actually wrong, and there is no file terminator in the data contained in the file. For getc, if it is not possible to read from a file, an integer-1 is returned, which is called EOF. The return EOF is nothing more than two cases, one is that the file has been read, and the second is the file read error, anyway, it is not read down.

usage Examples: Use fgetc and FPUTC to realize the function of file copy.

#include <stdio.h>

#include <stdlib.h>

int main (int argc, char *argv[])

{

FILE *FS,*FD;

int ch;

if (ARGC < 3)

{

fprintf (stderr, "Useage:%s<src_filename des_filename>", argv[0]);

Exit (1);

}

FS = fopen (Argv[1], "R");

if (fs = = NULL)

{

Perror ("fopen ()");

Exit (1);

}

FD = fopen (argv[2], "w");

if (fd = = NULL)

{

Fclose (FS); If the program is interrupted at this time, the open source should be released

Perror ("fopen ()");

Exit (1);

}

while (1)

{

CH = fgetc (FS);

if (ch = = EOF)

{

Break

}

FPUTC (CH,FD);

}

Fclose (FD);

Fclose (FS);

3.3, fputs and Fgets

function: Reads and writes a file as a string.

Usage:

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

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

Return value: Fgets the size bytes read from the *stream into *s,char* for write-back. Fputs return value is a reshape, success is non-negative, failed, return EOF

usage Examples: use fgets and fputs to realize the function of file copy.

#include <stdio.h>

#include <stdlib.h>

#define BUFSIZE 1024

int main (int argc, char **argv)

{

if (ARGC < 2)

{

fprintf (stderr, "usage....\n");

Exit (1);

}

Char Buf[bufsize];

FILE *FSP,*FDP;


FSP = fopen (Argv[1], "R");

if (FSP = = NULL)

{

Perror ("fopen ()");

Exit (1);

}

FDP = fopen (Argv[2], "w");

if (FDP = = NULL)

{

Fclose (FSP);

Perror ("fopen ()");

Exit (1);

}

while (fgets (BUF,BUFSIZE,FSP) = NULL)

{

Fputs (BUF,FDP);

}

Exit (0);

}


3.4 Fread and Fwrite

function: input and output of binary stream

Usage:

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


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

Note: size is the amount of each block, and NMEMB is the number of blocks.

Return value: The number of bytes read or written is "correct". 0 value returned after reading or error

Usage examples: use fread and fwrite to realize the function of file copy.

1 #include <stdio.h>

2 #include <stdlib.h>

3 #define BUFSIZE 1024

4 int main (int argc, char **argv)

5 {

6

7 if (ARGC < 3)

8 {

9 fprintf (stderr, "usage....\n");

Ten exit (1);

11}

FILE *FSP,*FDP;

-Char buf[bufsize];

N,res int;

FSP = fopen (Argv[1], "R");

if (FSP = = NULL)

17 {

Perror ("fopen ()");

Exit (1);

20}

FDP = fopen (Argv[2], "w");

if (FDP = = NULL)

23 {

Fclose (FSP);

Perror ("fopen ()");

+ exit (1);

27}

(n = fread (BUF,1,BUFSIZE,FSP)) > 0)

29 {

-res = fwrite (BUF,1,N,FDP);

n = n-res; Read all

if (n > 0)

33 {

(Res > 0)

35 {

res = fwrite (BUF,1,N,FDP);

Panax Notoginseng n = n-res;

38}

39}

40}

Exit (0);

42}

This article is from the "Hylinux" blog, make sure to keep this source http://hongyilinux.blog.51cto.com/8030513/1746199

A detailed description of Linux standard I/O functions

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.