The use of some advanced functions in C + +

Source: Internet
Author: User

setvbufName of function: setvbufFunction: Use the buffer in relation to the stream: int setvbuf (FILE *stream, char *buf, int type, unsigned size);Parameters: Stream: Pointer to stream, BUF: The address of the desired buffer, type: Expected buffer: _IOFBF (full buffer): reads the data from the stream when the buffer is empty. or write data to the stream when the buffer is full. _IOLBF (row buffer): Each time a row of data is read from the stream or a row of data is written to the stream. _IONBF (unbuffered): reads data directly from the stream or writes data directly to the stream without a buffer. Size: The number of bytes in the buffer. Note: This function should is called once the file associated with the stream have already been opened but before any input or Output operation have taken place. means that this function should be called immediately after opening the stream, fopen default buffer size and SETVBUF usage before any input output to the stream
/*Setvbuf Example*/#include<stdio.h>Charsbuf[1024x768];intMain () {FILE*Pfile;pfile=fopen ("MyFile.txt","W"); Setvbuf (PFile, Sbuf, _IOFBF,1024x768 );//File Operations herefclose (pFile);return 0;} In this example, write 1024 bytes each time, so only the buffer is full (with 1024 bytes) to write data to the file, you can reduce the number of IO

~/GCYIN/STUDY/PHP/TEST/DISCUZ/WEBBENCH-1.5/WEBBENCH.C:

F=fdopen (Mypipe[0], "R");
if (f==null)
{
Perror ("Open pipe for reading failed.");
return 3;
}
Setvbuf (f,null,_ionbf,0);


Getopt_long

Http://baike.baidu.com/view/906700.htm?fr=aladdin

features of the getopt () function provided by GNU

http://blog.csdn.net/realduke2000/article/details/1812126

Getopt is used to parse command-line option parameters. Getopt_long supports command-line parsing of long options, using man getopt_long to get its declaration as follows: int getopt_long (int argc, char * const ARGV[],CONST Char *optstring, const struct Option *longopts,int *longindex); argc and argv in functions are usually passed directly from the two parameters of Main (). Optsting is a string of option parameters: the string optstring can be the following elements: 1. Single character, 2. A single character followed by a colon: Indicates that the option must be followed by a parameter. Parameters are immediately followed by options or separated by a space. The pointer to the parameter is assigned to Optarg. 3 A single character followed by two colons, indicating that the option can have parameters or no parameters. If there are parameters, the parameters must be immediately followed by an option and cannot be separated by a space. The pointer to the parameter is assigned to Optarg. (This feature is the extension of GNU). Optstring is a string that represents the parameters that can be accepted. For example, "A:B:CD", which means that an acceptable parameter is a,b,c,d, where the A and B parameters are followed by more parameter values. (Example:-a host-b name) parameter longopts is actually an example of a struct: struct option {const char *name;//name represents a long parameter name int has_arg;//has_arg has 3 values, No_ argument (or 0), indicating that the parameter is not followed by the parameter value//Required_argument (or 1), indicating that the parameter must be followed by a parameter value//Optional_argument (or 2), indicating that the parameter can be followed, You can also not use the parameter value int *flag;//to determine what the return value of Getopt_long () is. If flag is null (typically), the function returns a Val value that matches the option of the item, and if flag is not NULL, the Val value is given the memory pointed to by flag, and the return value is set to 0. int Val; And Flag union determines the return value} parameter Longindex, which represents the index value of the current long parameter in longopts. [1]  gives an example: struct option long_options[] = {{"A123", required_argument, 0, ' a '},{"c123", No_argumENT, 0, ' C '},} now, if the command line parameter is-a 123, then call Getopt_long () will return the character ' a ', and the string 123 will be returned by OPTARG (note!) String 123 brought back by Optarg! Optarg does not need to be defined and is already defined in getopt.h), if the command-line argument is-C, then calling Getopt_long () returns the character ' C ', and at this point, Optarg is null. Finally, when Getopt_long () finishes parsing all parameters of the command line, it returns-1.  required_argument (or 1), the parameter input format is:--parameter value or--parameter = value. Optional_argument (or 2), the parameter input format can only be:--parameter = value.
Static Const structOption long_options[]={    {" Force", No_argument,&force,1},    {"Reload", No_argument,&force_reload,1},    {" Time", Required_argument,null,'T'},    {" Help", No_argument,null,'?'},    {"http09", No_argument,null,'9'},    {"HTTP10", No_argument,null,'1'},    {"HTTP11", No_argument,null,'2'},    {"Get",no_argument,&Method,method_get}, {"Head",no_argument,&Method,method_head}, {"Options",no_argument,&Method,method_options}, {"Trace",no_argument,&Method,method_trace}, {"version", No_argument,null,'V'},    {"Proxy", Required_argument,null,'P'},    {"Clients", Required_argument,null,'C'}, {NULL,0Null0}};Static voidUsagevoid) {fprintf (stderr,"webbench [option] ... Url\n"            "-f|--force Don ' t wait for reply from server.\n"            "-r|--reload Send Reload request-pragma:no-cache.\n"            "-t|--time <sec> Run benchmark for <sec> seconds. Default 30.\n"            "-p|--proxy <server:port> Use proxy server for request.\n"            "-c|--clients <n> Run <n> HTTP clients at once. Default one.\n"            "-9|--http09 use http/0.9 style requests.\n"            "-1|--http10 use http/1.0 protocol.\n"            "-2|--http11 use http/1.1 protocol.\n"            "--get use GET request method.\n"            "--head use head request method.\n"            "--options Use options request method.\n"            "--trace Use trace request method.\n"            "  -?| -h|--help this information.\n"            "-v|--version Display Program version.\n"           ); };intMainintargcChar*argv[]) {    intopt=0; intoptions_index=0; Char*tmp=NULL; if(argc==1) {usage (); return 2; }      while(Opt=getopt_long (ARGC,ARGV,"912vfrt:p:c:?h", long_options,&options_index))! =EOF) {        Switch(opt) { Case  0: Break;  Case 'F': force=1; Break;  Case 'R': force_reload=1; Break;  Case '9': http10=0; Break;  Case '1': http10=1; Break;  Case '2': http10=2; Break;  Case 'V': printf (program_version"\ n"); exit (0);  Case 'T': Benchtime=atoi (Optarg); Break;  Case 'P':                      /*Proxy Server parsing Server:port*/tmp=STRRCHR (Optarg,':'); ProxyHost=Optarg; if(tmp==NULL) {                           Break; }                      if(tmp==Optarg) {fprintf (stderr,"Error in option--proxy%s:missing hostname.\n", Optarg); return 2; }                      if(Tmp==optarg+strlen (Optarg)-1) {fprintf (stderr,"Error in option--proxy%s Port number is missing.\n", Optarg); return 2; }                      *tmp=' /'; ProxyPort=atoi (tmp+1); Break;  Case ':':             Case 'h':             Case '?': Usage ();return 2; Break;  Case 'C': Clients=atoi (Optarg); Break; }    }}

The use of some advanced functions in C + +

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.