|
Blue Forest http://www.lslnet.com at 10:18 on June 6, 2006
Gcc were formatted output function of (turn)
By scholar
"One";
1. Foreword
Programming in gcc, we can always use the characters in the formatted output function printf is, in fact inherited c gcc language processing is a strong function of style characters, it provided a series of formatted output function, the existence of two main documents stdio.h/ stdarg.h library functions. specific function is as follows :
[code]
#include
Printf, int printf (const char *format that. . . );
Fprintf, int fprintf (FILE *stream, const char *format that. . . );
Sprintf, int sprintf (char *str, const char *format that. . . );
Snprintf, int snprintf (char *str, size_t size, const char *format that. . . );
#include
Vprintf, vprintf (const char *format, va_list ap);
Vfprintf, vfprintf (FILE *stream, const char *format, va_list ap);
Vsprintf, int vsprintf (char *str, const char *format, va_list ap);
[/code]
Of which :
Printf and vprintf main function is to a standard output device or standard output format of the characters in the flow of characters.
Fprintf vfprintf function and the main characters is given to a flow of equipment output formatting characters.
And sprintf, snprintf, vsnprintf vsprintf function of the type and format the result to a string assignment.
Although there are differences of functions, the return value of the types vary, but in the creation of grammar or match the same format, these functions have some common features, which means that you must design templates and formatted string. These orders mainly through the string formatting function template string with the "%" behind the characters to control.
One example is as follows :
[code]
Int pct = 37;
Filename[] char = "foo.txt";
Printf ( "Processing of `%s' is %d%% finished.Please be patient. "Filename, pct);
[/code]
Obviously, this example is as follows : print results
Processing of `foo.txt 'is 37% finished.Please be patient.
As examples, we can see the general format of the grammar function features includes formatting string matching the output variable composition and the structure of the string.
2. Parameters details
As most parts of the grammar function in very similar ways formatted string, we first look at their common characteristics, and then using a variety of different examples to analyze its characteristics : Call the general format for such functions : printf ( "" format string "; "" Participation Questionnaire "; );
Which format string consists of two parts : part of the normal characters, these characters will form output; Another requirement is formatted characters, "%", the heel of one or more provisions of characters used to determine the output format of the content. Participation is required scale output range of parameters, which shows the number of string formatting of the output with the same number of parameters, the parameters "," separate, and he gathers the order, otherwise there will be an unexpected error.
Gcc 2.1 for the required format at :
%d
Symbols are integral metric
%i
Symbols are integral metric
Note : The top two in the formatted output function uses the same time, but they were not the same as the input, %i can accept any form of integer and %d does not.
%u
No symbol integer metric
%f
Output Float
%s
Output string
%c
Output individual characters
%p
Output value of the indicator
%e %E
Float forms of the index, which is lowercase forms : %e form of capital output is exported %E
%x, %X
No symbol to express the integral hexadecimal system, which is lowercase forms : %x form of capital output is exported %X
`%g '`%G'
According to the output data used to decide the size of an ordinary form or forms of output index, which is lowercase forms : %g form of capital output is exported %G
%o
No symbol to express the integral eight 229
`%c
'Export individual characters
%n
And the number of characters to be output, but the output of the parameters does not produce any results.
`%m '
The corresponding errors in the output string suggested.
`%%'
Output of the main characters "%" characters
Note :
In the "%" and said the number of letters between the largest market slipped wide. For example : three Integer %3d said output enough three right alignment. %9.2f Said nine export market for the float wide, which for two decimal places, rounded up as a six, representing a decimal point, not nine right alignment. Eight characters in the output string %8s said, not eight characters in the right alignment. If the string length, or more than cosmetic shows the median number of broad market, according to the length of the actual output. But to float, if integral part of the description than the median integral breadth run integer bit output; Note if the decimal part of the decimal point than the width of the median, according to the width due to rounding Note output. In addition, if the increase in output of some former 0, it should add a 0 before the presence of relief items. For example : %04d said in a less than four numerical output, it will be in front of premium 0 total width of four. If characters or plastic float, said the volume of output format, the figures represent the maximum width of decimal places, the figures represent the decimal point before the smallest width.
For example : %6.9s said that a length of not less than six string without exceeding 9. If more than nine, after the first nine characters will be deleted.
In the "%" between the letters and lowercase letters l, it is a long-output models.
For example : %ld double output, said output long integer %lf said Float
Alignment Alignment left or right to control output, "%" and the letters between adding a "-" available
Note alignment output left, right-alignment. For example : integer left seven %-7d said output, said output 10 characters left %-10s Alignment Alignment
2.2 provides a number of special characters
For firms
F-screen for page and
Enter
Tab Address
ASCII xhh said with a 16-chin said, which is one to two hh 16 nary |
Gcc were formatted output function of (turn)
"2";
3. Formatting changes in the specific details
Some 3.1 integer conversion
Integer conversion is a major part of the '%d,' '%i,' '%o,' '%u,' '%x', and '%X' order of a few parameters, different parameters, the results can be output in different formats. As shown in the table : '%d,' '%i' output is one of the few symbols of the metric system, '%o,' '%u', and '%x' output is one of the few non-symbolic, '%X' %x '' forms of capital. Which, in light of several different output parameters, there are the following items :
'-' Indicates the alignment of the left are generally the right alignment.
'+' To '%d,' '%i' in terms of two parameters, refer to the '+' symbols are few
'' For '%d,' '%i' in terms of two parameters, if the output is not '+' and '-' in the beginning, then start doing with the ballots.
'#' To '%o' parameters in terms of the output will result in a mandatory add '0' at the beginning.
'' 'Output figures will be used to LC_NUMERIC classification' and 'off.
'0' will be using the box '0' preference.
If not specified, was formatted as integer parameters were tacitly, or they can use the following parameters to the type designated amended, as follows :
'H' designated imported or unsigned short int int parameters of the short type of
'L' designated long parameters are imported or unsigned long int int types of
'Q' is a long long int parameters specified types of cases
'Z' designated size_t parameters are imported. .
To facilitate understanding gives an example :
Matching format for the following string :
"|%5d|%-5d|%+5d|%+-5d|% 5d|%05d|%5.0d|%5.2d|%d|"
The output will have a similar following :
| 0|0 | +0|+0 | 0|00000| | 00|0|
| 1|1 | +1|+1 | 1|00001| 1| 01|1|
| -1|-1 | -1|-1 | -1|-0001| -1| -01|-1|
|100000|100000|+100000| 100000|100000|100000|100000|100000|
Matching format for the following string :
"|%5u|%5o|%5x|%5X|%#5o|%#5x|%#5X|%#10.8x|"
The output will have a similar following :
| slope in northern hemisphere or the slope in northern hemisphere or the slope in northern hemisphere or the slope in northern hemisphere or the slope in northern hemisphere or 0x0| 0X0|0x00000000|
| Q q q q 01| 0x1| 0X1|0x00000001|
|100000|303240|186a0|186A0|0303240|0x186a0|0X186A0|0x000186a0|
Float some 320 conversion
Float some of the major change is' %f, '' %e, '' %E, '' %g ', and' %G '' order of a few parameters, different parameters, the results can be output in different formats. As shown in the table : '%f' output is a fixed form of Float : : which, in light of several different output parameters, there are the following items :
'-' Indicates the alignment of the left are generally the right alignment.
'+' Refers to the '+' symbols are few
'' If output is not '+' and '-' in the beginning, then start doing with the ballots,
'#' To '%g' and '%G' parameters in terms of the output will result in a mandatory add '0' at the beginning.
'' 'Output figures will be used to LC_NUMERIC classification' and 'off.
'0' box to the "0 'preference.
If not specified, were imported acquiescence double types of formatting parameters, the 'L' indicates a long double type.
Float formatted string example can be seen below :
The format string is as follows :
"|%12.4f|%12.4e|%12.4g|"
Output may arise as follows :
| slope in northern hemisphere or 0.0000| 0.0000e+00|
| Q 1.0000| 1.0000e+00|
| -1.0000| -1.0000e+00| -1|
| 100.0000| 1.0000e+02| 100|
| 1000.0000| 1.0000e+03| 1000|
| 10000.0000| 1.0000e+04| 1e+04|
| 12345.0000| 1.2345e+04| 1.234e+04|
| 100000.0000| 1.0000e+05| 1e+05|
| 123456.0000| 1.2346e+05| 1.234e+05|
Some 330 other format conversion
This part of the function of some relatively simple, as follows :
'%c' Output is a single string, formatted by the acquiescence of the output parameter is unsigned char type can be '-' indicates the alignment of the left. If not, the other parameters, such as :
Printf ( "%c%c%c%c%c", the 'h', 'e', 'l', 'l', 'o');
Displayed : 'Panorama'
'%s' Output is a string that. Acquiescence by the formatting of the output parameters char * (or const char *). Types can be '-' indicates the alignment of the left. If not, the other parameters, such as :
Printf ( "%3s%-6s", "no" and "where");
Displayed : 'nowhere'.
Note : If you use this parameter to a target range of formatting output parameters, which may be one '(null)' output. But sometimes the reason for the procedural guidelines for the air operation would have a "Segmentation fault" mistakes, the mistakes will be such an example below :
[code]
#include
Main ()
{
Char a;
A = inet_addr ( "192.168.1.1");
If (a!=-1) (
Printf ( "ip:%s", a) ;/* %s here may be wrong, it should use some better use %p */
}
}
[/code]
'%m' Output error messages. The following examples :
Fprintf (stderr, "can 't open' %s' : %m", filename);
Equivalent to the output of the order is as follows :
Fprintf (stderr, "can 't open' %s' : %s", filename, strerror (errno));
"%p" Parameter is the output target type, the formatting of imported silkworm room must be clear guidelines, "-" to show that the left alignment.
"%n" Is a more specific parameters, it does not affect formatting output, but output has been the length of the characters can be designated parameter type 'h' and 'l' to the designated output parameters are short and long int * * int types. In the example below :
Int nchar;
Printf ( "%d %s%n" 3 "bears" &nchar);
Output :
3 bears
7 exercises while the value of variables nchar.
'But I should warn that' output is "%" characters. |
Gcc were formatted output function of (turn)
"3";
4. Function brief
4.1printf () Function
Printf () function is formatted output function is generally characterized by a series of more general standards for the export of equipment to the required output format information. Often used in the preparation of procedures now function. Printf () function call format :
Printf ( "" format string "; "" Participation Questionnaire "; );
[code]
#include
#include
Int main ()
{
Char c, s[20], *p;
Int a=1234, *i;
Float f=3.141592653589;
Double x=0.12345678987654321;
P "How do you do";
Strcpy (s, "Hello, Comrade");
*i=12;
= 'X41';
Printf ( "a=%d", a); LEAVES OF 13 SPECIES OF LAURACEAE output metric integer results a=1234*/
Printf ( "a=%6d", a); LEAVES OF 13 SPECIES OF LAURACEAE six metric few results output = 1234*/
Printf ( "a=%06d", a); LEAVES OF 13 SPECIES OF LAURACEAE six metric few a=001234*/ output results
Printf ( "a=%2d", a); /*a Over two spaces, according to the actual value of output a=1234*/
Printf ( "*i=%4d" *i); LEAVES OF 13 SPECIES OF LAURACEAE four metric output * i = integer 12*/
Printf ( "*i=%-4d" *i); LEAVES OF 13 SPECIES OF LAURACEAE output left four metric integral alignment *i=12*/
Printf ( "i=%p", i); LEAVES OF 13 SPECIES OF LAURACEAE output addresses i=06E4*/
Printf ( "f=%f", f); LEAVES OF 13 SPECIES OF LAURACEAE output Float f=3.141593*/
Printf ( "f=6.4f", f); LEAVES OF 13 SPECIES OF LAURACEAE output of four decimal places in which six float f=3.1416*/
Printf ( "x=%lf", x); LEAVES OF 13 SPECIES OF LAURACEAE output x=0.123457*/ long Float
Printf ( "x=%18.16lf", x) 18 ;/* output which the decimal point after 16 long float x=0.1234567898765432*/
Printf ( "c=%c" c); LEAVES OF 13 SPECIES OF LAURACEAE output characters c=A*/
Printf ( "c=%x" c); ASCII characters in the output value c=41*/ LEAVES OF 13 SPECIES OF LAURACEAE
Printf ( "s[]=%s", s); LEAVES OF 13 SPECIES OF LAURACEAE s[]=Hello string array output, Comrade*/
Printf ( "s[]=%6.9s", s) ;/* up to nine characters in the output string s[]=Hello, Co*/
Printf ( "s=%p", s); LEAVES OF 13 SPECIES OF LAURACEAE first character string array output addresses s=FFBE*/
Printf ( "*p=%s", p); LEAVES OF 13 SPECIES OF LAURACEAE output target string p=How do you do*/
Printf ( "p=%p", p); LEAVES OF 13 SPECIES OF LAURACEAE target output value p=0194*/
Getch ();
Retunr 0
}
[/code]
The above results may be different computers at different addresses.
The first example is the meaning of the phrase #include stdio.h Calling another document, which is a first, which includes all the standard library functions of the input and output data types and function definition shows. Each library functions used to the types of variables and functions have been defined and that the first paper on the corresponding "*.h", the user must use these functions used #include<*.h>; or #include "*.h" in the first sentence Call corresponding documents for use if there is no note of this sentence, it will link errors.
4.2 fprintf () function.
Fprintf () function formatted with the printf () function the same, the difference lies only in fprintf () function is included to the documents. And printf () to screen output.
The following are an example of a post-operation after test.dat document.
[code]
#include
Main ()
{
Char *s= "That 's good news") LEAVES OF 13 SPECIES OF LAURACEAE target definition and initialization string */
Int integer variable definition and initialization */ i=617; LEAVES OF 13 SPECIES OF LAURACEAE
FILE *fp; LEAVES OF 13 SPECIES OF LAURACEAE guideline document definition */
Fp=fopne ( "test.dat", "w"); LEAVES OF 13 SPECIES OF LAURACEAE creation of a written document written only */
Fputs ( "Your score of TOEFLis", fp) ;/* built to a string of characters into documents */
Fputc ( ':', fp); LEAVES OF 13 SPECIES OF LAURACEAE built to write papers colon : */
Fprintf (fp, "%d", i); LEAVES OF 13 SPECIES OF LAURACEAE built to write a paper a few cosmetic */
Fprintf (fp, "%s", s); LEAVES OF 13 SPECIES OF LAURACEAE built to write a paper string */
Fclose (fp); LEAVES OF 13 SPECIES OF LAURACEAE closing documents */
}
[/code]
CAT order showed TEST.DAT display the content as follows :
Your TOEFL score of 617 is :
That 's good news
4.3 sprintf () function.
Sprintf (string, fmt,. . . ) Is the string back to the type of array and space characters to the end. However, the distribution function is likely to exceed the length of characters. More dangerous. Below is a sprintf () are a case in point.
[code]
Int
4003rd come under the Mission pyramid data structure, creating socket link, obtained the document size.
Get_size_of_url (struct Mission* pms)
{
Int s;
Struct sockaddr_in sin;
Struct hostent* phe;
Char cmd[256];
Char msg_hdr[1000];
Char* sources
4003rd preparing http GET method at the request of China.
Sprintf (cmd, "GET %s HTTP/1.0" pms->;url);
4003rd creating socket
If ((s=socket (PF_INET, SOCK_STREAM,0)) "0)
Return 1;
4003rd made remote server IP address, but failed to return -1 function
If ((phe = gethostbyname (pms->;host)) ====== NULL)
Return 1;
Memset (&sin,0, sizeof (sin));
Memcpy (&sin.sin_addr, phe->;h_addr, sizeof (struct in_addr));
Sin.sin_family=AF_INET;
Sin.sin_port=htons (pms->;port);
4003rd establish links with remote machines failed to return -1 function
If (connect (s, (struct sockaddr*) &sin, sizeof (sin)) ==-1)
Return 1;
4003rd sending GET request
If (write (s, cmd, strlen (cmd)) "0)
Return 0
4003rd from the link descriptors (connecting pipe) to read the data transmission over
If (read (s, msg_hdr, 300) "0)
Return 0
Close (s);
Printf ( "%s" msg_hdr);
4003rd read the paper size
If ((p=strstr (msg_hdr "Content-Length")) | | (p=strstr (msg_hdr "Content-length :")))
P+=16;
Else
Return 0
4003rd return to size
Return atoi (p);
}
[/code]
Note : In most Unix systems, sprintf (string, fmt,. . . ) Back to the string of indicators, however, this area of Linux (follow ANSI) is placed in the back of the character string number. Transplants, especially against SunOS. the need for vigilance in the streets.
4.4 Snprintf () Function
Snprintf () function and Sprintf () function is very similar, but the size of the multi-function parameters to show the largest number of characters, the function return values indicate a whole number of the characters were stored, said output return -1 enough space for the characters. The following examples :
[code]
Char *
Make_message (char *name, char *value)
{
LEAVES OF 13 SPECIES OF LAURACEAE pre-allocated space for 100 characters. */
Int size = 100;
Char *buffer = (char *) xmalloc (size);
While (1)
{
LEAVES OF 13 SPECIES OF LAURACEAE characters to the output format for the space. */
Int nchars = snprintf (buffer, size, "value of %s is %s", name, value);
LEAVES OF 13 SPECIES OF LAURACEAE determine whether to return to the true values */
If (nchars "size)
Return buffer;
LEAVES OF 13 SPECIES OF LAURACEAE if enough space, distribution space to the two-fold increase in advance */
Size *= 2;
Buffer = (char *) xrealloc (size, buffer);
}
}
[/code]
4.5 asprintf () function.
Int asprintf (char **ptr, const char *template that. . . )
With the function sprintf () function is very similar, but it will change the distribution dynamic allocation in the form of string parameter is a char * ptr address of the target function to return at a new target. The following examples :
[code]
LEAVES OF 13 SPECIES OF LAURACEAE Construct a message describing the value of a variable whose name is name and whose value is value. */
Char *
Make_message (char *name, char *value)
{
Char *result;
Asprintf (&result "value of %s is %s", name, value);
Return result;
}
[/code]
4.6 Vprintf () Function
Int vprintf (const char *template, va_list ap)
Printf function with the function is very similar, just the number of variable parameters, the list has become a target.
4.7 Vfprintf () Function
Int vfprintf (FILE *stream, const char *template, va_list ap)
Fprintf function with the function is very similar, just the number of variable parameters, the list has become a target.
4.8 vfprintf () Function
Int vsprintf (char buried, const char *template, va_list ap)
Sprintf function with the function is very similar, just the number of variable parameters, the list has become a target.
4.9 vsnprintf () function.
Int vsnprintf (char buried, size_t size, const char *template, va_list ap)
Snprintf function with the function is very similar, just the number of variable parameters, the list has become a target.
4.10 vasprintf () Function
Int vasprintf (char **ptr, const char *template, va_list ap)
Asprintf function with the function is very similar, just the number of variable parameters, the list has become a target. |
Gcc were formatted output function of (turn)
UP about |
Gcc were formatted output function of (turn)
Put very wide indeed, many of us do not usually pay attention! ! ! |
Gcc were formatted output function of (turn)
Experts :
Details can fscanf () function is used, in particular, for example, focused on the format
Setting; |
Gcc were formatted output function of (turn)
SCANF used in the same way and
Try to write their own procedures use the format listed above |
Gcc were formatted output function of (turn)
Thank you!
But I was still a student, not that CRC.
Do you really understand more! Good envy! I have time to teach us? (Paranoia)
Kazakhstan, thank you!
Although I am still confused.
But
Fortunately, the
It was the same and I
Desertification (I)
Lane
!
Very grateful days! |
| |