I/O function summary

Source: Internet
Author: User
Tags modifiers sprintf

After a period of study, found that character processing or file processing, often need to input (read in) and output, and to choose a lot of functions, and now it is easy to confuse, the following on the commonly used input and output functions to summarize and compare, in order to distinguish and master.

Standard printf (), scanf (); GetChar (), Putchar (); Gets (), puts ();
File fprintf (), fscanf (); Getc (), PUTC (); Fgets (), fputs ();
Other sprintf ();

1.printf (), scanf ()

This is the first pair of input and output functions to learn the C language contact.

printf () Basic usage: printf (Control string, parameter list)

Where the control string is the statement in double quotation marks, the string or content to be printed, and the parameter list is the specific parameter. The control string involves conversion descriptions, modifiers, and flags. For example: printf ("*%-10.2f\n*", 3.14), where "-" is marked (left-aligned), "10.2" is decorated (10 characters justifies, the decimal point is two-bit), "%f" is the conversion specifier (floating-point number), so the output is *3.14 *. For specific conversion instructions, modifiers, and flags, you can refer to the 68/70/71 page of Cprimerplus.

scanf () Basic usage: scanf (Control string, parameter list)

Where the control string is also the contents of the double quotation marks, but usually only contains the conversion description conforms to the conversion modifier; The argument list is a pointer to the variable. For example: scanf ("%d,%d", &m, &n), will read 88, 121 of these two inputs (commas are also required to enter). The scanf () function returns a value that corresponds to the number of items successfully read into.

2.fprintf (), fscanf ()

fprintf () works in a similar way to printf (), but requires a parameter (which can be a file pointer, standard input, output, error) to specify the appropriate file. For example: fprintf (FP, "Tom is%d years old", 24) refers to input from the file FP Tom is to years old.

FSCANF () works in a similar way to scanf () and requires a parameter to make the appropriate file. For example: fscanf (FP, "%s", words) refers to a string words that is read from the file FP.

3.sprintf ()

The function is the same as printf (), but it is written in a string rather than written to the output display. Therefore, it provides a way to combine several elements into a single string. The first parameter of sprintf () is the address of the destination string, and the rest of the arguments are the same as printf ().

For example:

" Tom "  "Smith" """  %s%s", first, last);

The new total is "Name:tomsmith".

4.getchar (), Putchar ()

GetChar () and Putchar () are single-character I/O functions, which can only input or output one character at a time.

For example:

Char= getchar ();p Utchar (CH);

That is, a character is read into Ch by the keyboard, and the character is output.

5.getc (), PUTC ()

Getc () and GetChar () are similar, just need to tell Getc () The file it is going to use. For example: Ch=getc (FP), the GETC () function obtains a character from the FP file and assigns a value to Ch.

Similar to PUTC () and Putchar (), it is also necessary to tell PUTC () the files it needs. For example: PUTC (ch,fpout), the PUTC () function outputs a CH character and is entered into the Fpout file.

6.gets (), puts ()

Get () gets a string from the system's standard input device (keyboard), which reads all the characters before the newline character, adds a null character after the characters, and assigns the corresponding string address.

The puts () function is simple to use, just give the address of the string argument, output the string, and automatically add a newline at the end.

7.fgets (), fputs ()

Fgets () has three parameters, that is, the assigned character array address, the maximum number of read-in strings, and the file address (which file to read). For example: Fgets (BUF, FP) is a line in the FP file (the first newline match either) is read into and added to the above characters, and finally assigned to BUF.

The fputs () function accepts two parameters, that is, the address of a string and a file pointer. It writes the string that the string address pointer refers to to the specified file. However, it does not automatically add line breaks after the string is finished.

I/O function summary

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.