标签:
#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[409600]; char cpline[1024]; FILE *fp; va_list val; va_start(val, cpFormat); vsnprintf(cpcmd, sizeof(cpcmd)-1, cpFormat, val); va_end(val); fp = popen(cpcmd, "r"); while (fgets(cpline, 1023, 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) == -1) return false; return (!( st.st_mode & S_IFDIR)); } int fileSize(const char *cpfilename) { struct stat st; // 须处理符号链接文件 if (stat(cpfilename, &st) != 0) return -1; return st.st_size; } int main(int argc, char*argv[]) { string arg = argv[1]; 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 == -1) { 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() != 0) { printf("size is %d\n", atoi(val.c_str())); } else { printf("error\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/foreverstars/p/4798636.html