最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
linux c++ 两个实用函数实例分享
时间:2022-06-25 04:47:20 编辑:袖梨 来源:一聚教程网
linux c++ 获取基本类型的极值
源代码
#include#include #include using namespace std; int main() { cout << "bool: tt" << "所占字节数:" << sizeof(bool); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "char: tt" << "所占字节数:" << sizeof(char); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "signed char: t" << "所占字节数:" << sizeof(signed char); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "unsigned char: t" << "所占字节数:" << sizeof(unsigned char); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "wchar_t: t" << "所占字节数:" << sizeof(wchar_t); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "short: tt" << "所占字节数:" << sizeof(short); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "int: tt" << "所占字节数:" << sizeof(int); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "unsigned: t" << "所占字节数:" << sizeof(unsigned); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "long long: tt" << "所占字节数:" << sizeof(long long); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "long: tt" << "所占字节数:" << sizeof(long); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "unsigned long: t" << "所占字节数:" << sizeof(unsigned long); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "double: t" << "所占字节数:" << sizeof(double); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "long double: t" << "所占字节数:" << sizeof(long double); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "float: tt" << "所占字节数:" << sizeof(float); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "size_t: t" << "所占字节数:" << sizeof(size_t); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "string: t" << "所占字节数:" << sizeof(string) << endl; return 0; }
Linux中用C++通过管道来获取命令行输出的内容
使用C++时,有些时候我们可以通过管道的方法来获取所执行的命令所输出的内容。具体做法如下:
首先你需要确定你所执行的命令所输出的内容的格式(不同的操作系统相同的命令所输出来的内容格式可能会不一致)。我们来举例说明:我想获取/var目录当前所暂用的存储空间,以KB为单位输出:
[root@localhost /]# du -sk /var
13532132 var
[root@localhost /]# du -sk /var | awk -F ' ' '{print $1}'
13532132
从上面输出可以知道/var目录当前暂用了13532132KB,那么我们如何将改值使用到我们的程序中呢?为了简单起见我实现了如下函数:
int64_t CalculateDirUsedCapacity (const std::string& directoryName) { FILE *fstream = NULL; char buff[32]; memset (buff ,'