• system, fileExist函数包装


    #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 
    ", file.c_str(), size);
        }
        else
        {
            printf("file exists! name: %s size: %d
    ", file.c_str(), size);
        }
    
    
        int fd = open(file.c_str(), O_RDONLY);
        if(fd == -1)
        {
            printf("can not open file: %d
    ", fd);
        }
        else
        {
            printf("open file %d
    ", 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
    ", atoi(val.c_str()));
        }
        else
        {
            printf("error
    ");
        }
        return 0;
    }
  • 相关阅读:
    React-Native: bios打开VT-x选项
    React-Native:解决真机调试时候Could not get BatchedBridge, make sure your bundle is packaged properly
    node.js异步编程的几种模式
    第29章 电容触摸屏—触摸画板
    第28章 LTDC—液晶显示中英文
    第27章 LTDC/DMA2D—液晶显示
    第26章 FMC—扩展外部SDRAM
    第25章 串行FLASH文件系统FatFs
    第24章 QSPI—读写串行FLASH
    第23章 I2C—读写EEPR
  • 原文地址:https://www.cnblogs.com/foreverstars/p/4798636.html
Copyright © 2020-2023  润新知