system, fileExist函数包装

时间:2021-10-18 03:33:08
#include "stdio.h"
#include <string>
#include<sys/types.h>
#include<fcntl.h>
#include <sys/stat.h>
using std::string; #include <iostream>
#include <map>
#include <string>
#include <sys/time.h>
#include <errno.h>
#include <iconv.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/select.h>
#include <sys/time.h> string System(string &strrtn, const char *cpFormat, ...)
{
if (cpFormat == NULL)
return strrtn; char cpcmd[];
char cpline[];
FILE *fp;
va_list val; va_start(val, cpFormat);
vsnprintf(cpcmd, sizeof(cpcmd)-, cpFormat, val);
va_end(val); fp = popen(cpcmd, "r"); while (fgets(cpline, , fp) != NULL)
{
strrtn += cpline;
}
pclose(fp);
return strrtn;
} bool fileExist(const char *cpfile)
{
if (NULL == cpfile)
return false; struct stat st;
if (stat(cpfile, &st) == -)
return false; return (!( st.st_mode & S_IFDIR));
} int fileSize(const char *cpfilename)
{
struct stat st; // 须处理符号链接文件
if (stat(cpfilename, &st) != )
return -;
return st.st_size;
} int main(int argc, char*argv[])
{
string arg = argv[];
string fileName = arg;
string path = "";
string file = path + fileName; int size = fileSize(file.c_str()); if(!fileExist(file.c_str()))
{
printf("The file not exists! name: %s size: %d \n", file.c_str(), size);
}
else
{
printf("file exists! name: %s size: %d\n", file.c_str(), size);
} int fd = open(file.c_str(), O_RDONLY);
if(fd == -)
{
printf("can not open file: %d\n", fd);
}
else
{
printf("open file %d\n", fd);
} string calsize = "ls -l " + fileName + " | awk '{print $5}'";
string val;
val = System(val, calsize.c_str());
if(val.size() != )
{
printf("size is %d\n", atoi(val.c_str()));
}
else
{
printf("error\n");
}
return ;
}