最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
linux c++ 两个实用函数实例分享
时间:2015-07-19 编辑:简简单单 来源:一聚教程网
linux c++ 获取基本类型的极值
源代码
#include#include #include using namespace std; int main() { cout << "bool: \t\t" << "所占字节数:" << sizeof(bool); cout << "\t最大值:" << (numeric_limits ::max)(); cout << "\t\t最小值:" << (numeric_limits ::min)() << endl; cout << "char: \t\t" << "所占字节数:" << sizeof(char); cout << "\t最大值:" << (numeric_limits ::max)(); cout << "\t\t最小值:" << (numeric_limits ::min)() << endl; cout << "signed char: \t" << "所占字节数:" << sizeof(signed char); cout << "\t最大值:" << (numeric_limits ::max)(); cout << "\t\t最小值:" << (numeric_limits ::min)() << endl; cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char); cout << "\t最大值:" << (numeric_limits ::max)(); cout << "\t\t最小值:" << (numeric_limits ::min)() << endl; cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t); cout << "\t最大值:" << (numeric_limits ::max)(); cout << "\t\t最小值:" << (numeric_limits ::min)() << endl; cout << "short: \t\t" << "所占字节数:" << sizeof(short); cout << "\t最大值:" << (numeric_limits ::max)(); cout << "\t\t最小值:" << (numeric_limits ::min)() << endl; cout << "int: \t\t" << "所占字节数:" << 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: \t\t" << "所占字节数:" << sizeof(long long); cout << "\t最大值:" << (numeric_limits ::max)(); cout << "\t最小值:" << (numeric_limits ::min)() << endl; cout << "long: \t\t" << "所占字节数:" << 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: \t\t" << "所占字节数:" << 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 ,'\0', sizeof(buff)); std::string cmd = "du -sk " + directoryName; cmd += "| awk -F ' ' '{print $1}'"; // 通过管道来回去系统命令返回的值 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; }
相关文章
- C#复制数组的两种方式及效率比较解读 10-24
- ASP.NET Identity用法解析 10-24
- ASP.NET MVC使用Identity增删改查用户介绍 10-24
- C语言中atoi函数模拟实现介绍 10-18
- .Net反向代理组件Yarp用法介绍 10-10
- .NET使用YARP通过编码方式配置域名转发实现反向代理教程 10-10