#include <stdio.h>
#include <stdlib.h>#include <string.h>
int main()
{
char buff[256];
FILE* fd;
int nTotal;
int nUsed;
char *pLine;
if(system("free -m | awk '{print $2,$3}' > mem2"));memset(buff, 0, 256);
if(fd = fopen("mem2", "rb") < 0){
return -1;
}
fread(buff,1, sizeof(buff) , fd);pLine = strstr(buff, "\n");
nTotal= atoi(pLine);
pLine= strstr(pLine, " ");
nUsed= atoi(pLine);
memset(buff, 0, 256);
sprintf(buff, "Used %dM / Total %dM\n", nUsed, nTotal);
if(strlen(buff) > len)
{
return -1;
}
printf("%s", buf);
fclose(fd);
return 0;}
运行结果:Used 500M / Total 967M