Linux c ++ two practical function instances

Source: Internet
Author: User

Linux c ++ obtains the extreme values of Basic types

Source code

# Include <iostream> # include <string. h> # include <limits> using namespace std; int main () {cout <"bool: \ t" <"bytes: "<sizeof (bool); cout <" \ t maximum value: "<(numeric_limits <bool>: max )(); cout <"\ t minimum value:" <(numeric_limits <bool>: min) () <endl; cout <"char: \ t "<" bytes: "<sizeof (char); cout <" \ t maximum value: "<(numeric_limits <char>: max) (); cout <"\ t minimum value:" <(numeric_limits <char>: min) () <endl; cout <"signed char: \ t "<" bytes: "<sizeof (signed char); cout <" \ t maximum value: "<(numeric_limits <signed char>: max) (); cout <"\ t minimum value:" <(numeric_limits <signed char >:: min) () <endl; cout <"unsigned char: \ t "<" bytes: "<sizeof (unsigned char); cout <" \ t maximum value: "<(numeric_limits <unsigned char >:: max) (); cout <"\ t minimum value:" <(numeric_limits <unsigned char >:: min) () <endl; cout <"wchar_t: \ t "<" bytes: "<sizeof (wchar_t); cout <" \ t maximum value: "<(numeric_limits <wchar_t>: max) (); cout <"\ t minimum value:" <(numeric_limits <wchar_t>: min) () <endl; cout <"short: \ t "<" bytes: "<sizeof (short); cout <" \ t maximum value: "<(numeric_limits <short>: max) (); cout <"\ t minimum value:" <(numeric_limits <short>: min) () <endl; cout <"int: \ t "<" bytes: "<sizeof (int); cout <" \ t maximum value: "<(numeric_limits <int>: max) (); cout <"\ t minimum value:" <(numeric_limits <int>: min) () <endl; cout <"unsigned: \ t "<" bytes: "<sizeof (unsigned); cout <" \ t maximum value: "<(numeric_limits <unsigned>: max) (); cout <"\ t minimum value:" <(numeric_limits <unsigned>: min) () <endl; cout <"long: \ t "<" bytes: "<sizeof (long); cout <" \ t maximum value: "<(numeric_limits <long> :: max) (); cout <"\ t minimum value:" <(numeric_limits <long>: min) () <endl; cout <"long: \ t "<" bytes: "<sizeof (long); cout <" \ t maximum value: "<(numeric_limits <long>: max) (); cout <"\ t minimum value:" <(numeric_limits <long>: min) () <endl; cout <"unsigned long: \ t "<" number of bytes: "<sizeof (unsigned long); cout <" \ t maximum value: "<(numeric_limits <unsigned long>: max) (); cout <"\ t minimum value:" <(numeric_limits <unsigned long>: min) () <endl; cout <"double: \ t "<" number of bytes: "<sizeof (double); cout <" \ t maximum value: "<(numeric_limits <double>: max) (); cout <"\ t minimum value:" <(numeric_limits <double >:: min) () <endl; cout <"long double: \ t "<" number of bytes: "<sizeof (long double); cout <" \ t maximum value: "<(numeric_limits <long double>: max) (); cout <"\ t minimum value:" <(numeric_limits <long double>: min) () <endl; cout <"float: \ t "<" bytes: "<sizeof (float); cout <" \ t maximum value: "<(numeric_limits <float>: max) (); cout <"\ t minimum value:" <(numeric_limits <float>: min) () <endl; cout <"size_t: \ t "<" bytes: "<sizeof (size_t); cout <" \ t maximum value: "<(numeric_limits <size_t>: max) (); cout <"\ t minimum value:" <(numeric_limits <size_t>: min) () <endl; cout <"string: \ t "<" bytes: "<sizeof (string) <endl; return 0 ;}




In Linux, you can use C ++ to obtain command line output content through pipelines.

When using C ++, sometimes we can use the pipeline method to obtain the output content of the executed command. The procedure is as follows:
First, you need to determine the format of the content output by the command you run (the format of the content output by the same command in different operating systems may be different ). Here is an example: I want to get the temporary storage space of the/var Directory, measured in KB:

[Root @ localhost/] # du-sk/var
13532132 var
[Root @ localhost/] # du-sk/var | awk-F ''' {print $1 }'
13532132
From the above output, we can know that the/var directory currently uses 13532132KB. How can we use the modified value in our program? For simplicity, I implemented the following functions:

Int64_t CalculateDirUsedCapacity (const std: string & directoryName) {FILE * fstream = NULL; char buff [32]; memset (buff, '\ 0', sizeof (buff )); std: string cmd = "du-sk" + directoryName; cmd + = "| awk-F ''' {print $1 }'"; // return the value if (NULL = (fstream = popen (cmd. c_str (), "r") {return 0;} if (NULL = fgets (buff, sizeof (buff), fstream) {pclose (fstream ); return 0;} std: strstream ss; ss <buff; int64_t usedCapacity = 0; ss> usedCapacity; pclose (fstream); // close pipe return usedCapacity ;}


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.