C Language Output

Source: Internet
Author: User
Printf is the output function of the standard output stream. It is used to output data to a standard output device such as the screen, while fprintf outputs data to a file,
Outputs the output content to a file on the hard disk or printf is a buffer output on the device that is equivalent to the file. fprintf does not buffer fprintf () and transmits it to the original function of a stream: int fprintf (File * stream, const char * Format [, argument,...]) function <javascript: void (0)>: ① output ASC ⅱ code to the file pointed to by the file pointer ② output error information to the display <javascript: void (0)> usage form: ① fprintf (File pointer, "output format", output item series); ② fprintf (stderr, "error message"); Use instance: fprintf (stream, "% d % C % F", I, C, F); defines file * stream; int I; char C; float F; return value: if the result is successful, the number of output bytes is returned. If the result is incorrect, the EOF is returned. Header file: stdio. h ======================================================== ============= sprintf () format the function output to the array: int sprintf (BUF, format, arg_list) Where char * Buf; function: output the value of the arg_list parameter to the Buf in format. Example: sprintf (buffer, "relative sector code: % s (wherein, the cylinder number is % d/The head number is % d/The sector number is % d)", String, cyls, Head, sector), where Char buffer [100]; char * string = ""; int cyls, Head, sector; return value: the number of characters actually written to the array. Header file: stdio. h sprintf is a Variable Parameter Function, defined as: int sprintf (char * buffer, const char * Format [, argument]...); in addition to the fixed types of the first two parameters, you can take over multiple parameters later. Its essence is obviously on the second parameter: Format String. Both printf and sprintf use formatted strings to specify the string format. Some format specifiers (format specifications) starting with "%" are used inside the format string to occupy a position, the variable is provided in the variable parameter list, and the function will replace the specifier with the variable at the corresponding position to generate the string that the caller wants. One of the most common applications of formatting a numeric string sprintf is to print Integers to strings .... the formatting functions fscanf and fprintf fscanf are similar to those of the previously used scanf and printf functions, both of which are formatting functions.
The difference between the fscanf function and fprintf function is that the Read and Write objects are not keyboard and display, but disk files. The call formats of these two functions are:
Fscanf (File pointer, Format String, input table column); fprintf (File pointer, Format String, output table column); for example: fscanf (FP, "% d % s", & I, s); fprintf (FP, "% d % C", J, CH); Use the fscanf and fprintf functions to complete the problem in case 10.6. Shows the modified program in example 10.7. [Example 10.7] # include <stdio. h> struct Stu {char name [10]; int num; int age; char ADDR [15];} Boya [2], boyb [2], * PP, * QQ; main () {file * FP; char ch; int I; pp = Boya; QQ = boyb; If (FP = fopen ("stu_list", "WB + ")) = NULL) {printf ("cannot open file strike any key exit! "); Getch (); exit (1);} printf (" \ ninput data \ n "); for (I = 0; I <2; I ++, PP ++) scanf ("% S % d % s", PP-> name, & PP-> num, & PP-> Age, PP-> ADDR ); pp = Boya; for (I = 0; I <2; I ++, pp ++) fprintf (FP, "% S % d % s \ n ", PP-> name, PP-> num, PP-> Age, PP-> ADDR); rewind (FP); for (I = 0; I <2; I ++, qq ++) fscanf (FP, "% S % d % s \ n", QQ-> name, & QQ-> num, & QQ-> Age, qq-> ADDR); printf ("\ n \ nname \ tnumber age ADDR \ n"); QQ = boyb; for (I = 0; I <2; I ++, qq ++) printf ("% s \ t % 5d % 7D % s \ n", QQ-> name, QQ-> num, QQ-> Age, qq-> ADDR); fclose (FP );}

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.