Comparison of file input/output functions

Source: Internet
Author: User
Tags form post

[Problem description] the following definitions are common: fgets ()/fputs (), gets ()/puts (), getchar ()/putchar (), fgetc () /fputc (), getc ()/putc (), fscanf ()/scanf ()/sscanf (). What is the difference between them?
[Analysis]
1. function declaration
Glibc-2.3.6/Libio/stdio. h
Fgets ()/fputs ()
[Html]
1. extern char * fgets (char * _ restrict _ s, int _ n, FILE * _ restrict _ stream );
2. extern int fputs (_ const char * _ restrict _ s, FILE * _ restrict _ stream );
 
Gets ()/puts ()
[Html]
1. extern char * gets (char * _ s );
2. extern int puts (_ const char * _ s );
 
Getchar ()/putchar ()
[Html]
1. extern int getchar (void );
2. extern int putchar (int _ c );

Fgetc ()/fputc ()
[Html]
1. extern int fgetc (FILE * _ stream );
2. extern int fputc (int _ c, FILE * _ stream );
Getc ()/putc ()
[Html]
1. extern int getc (FILE * _ stream );
2. extern int putc (int _ c, FILE * _ stream );

Fscanf ()/scanf ()/sscanf ()
[Html]
1. extern int fscanf (FILE * _ restrict _ stream,
2. _ const char * _ restrict _ format ,...);
3.
4. extern int scanf (_ const char * _ restrict _ format ,...);
5./* Read formatted input from S .*/
6. extern int sscanf (_ const char * _ restrict _ s,
7. _ const char * _ restrict _ format,...) _ THROW;

2 important differences
(1) fgets ()/gets ()
From the Declaration, we can see that the fgets () parameter has the input length limit, but gets () does not. Therefore, fgets () can prevent storage overflow. Therefore, fgets is a better choice for strict programming. Fgets () and fputs () must be paired. Because when fgets () is read, it will read the linefeed into the string, while gets () will not. In the output, puts () append a line break, while fputs () does not append a line break. If fgets () is used for input and puts () for output, input a line break and the output will have two line breaks.
(2) function or macro definition
Both fgetc () and fputc () are real functions, while getc, putc, getchar, and putchar are macros defined by the # define command. Macro is inserted into the code During preprocessing, And the execution efficiency is slightly higher. Functions are superior in program length.
Getc prototype www.2cto.com
[Html]
1. Glibc-2.3.6/libio/getc. c
2. int
3. _ IO_getc (fp)
4. FILE * fp;
5 .{
6. int result;
7. CHECK_FILE (fp, EOF );
8. _ IO_acquire_lock (fp );
9. result = _ IO_getc_unlocked (fp );
10. _ IO_release_lock (fp );
11. return result;
12 .}

Putc prototype
[Html]
1. Glibc-2.3.6/sysdeps/generic/printf_fphex.c
2. # define putc (c, f) (wide \
3 .? (Int) _ IO_putwc_unlocked (c, f): _ IO_putc_unlocked (c, f ))
Getchar prototype
[Html]
1. Glibc-2.3.6/libio/bits/stdio. h
2. _ STDIO_INLINE int
3. getchar (void)
4 .{
5. return _ IO_getc (stdin );
6 .}

Putchar prototype
[Html]
1. Glibc-2.3.6/libio/bits/stdio. h
2.
3. _ STDIO_INLINE int
4. putchar (int _ c)
5 .{
6. return _ IO_putc (_ c, stdout );
7 .}
 
(3) Difference in getchar ()/fgets () Usage
Getchar () can only enter one character at a time. putchar () is used to output the return value of getchar. Note that the returned value is int rather than char. In this case, it is used to receive characters in a more effective manner, because char can only represent 8-bit unsigned numbers, if the length exceeds 8 characters, it cannot be expressed. Int can receive 8 values that cannot be expressed by the unsigned number. Fgets and gets can input multiple characters at a time, using fputs and puts respectively.
(4) scanf/fscanf/sscanf
For the differences between scanf and printf, see scanf and printf conversion specifiers.
For an example of sscanf, see: How does cgi extract special substrings of form post?
Scanf reads data from standard input. The input source of fscanf is the stream given as a parameter; sscanf is the format output, which can replace the specified flow with the required format.

 
From tandesir's column

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