美文网首页
判断文件是否是pdf

判断文件是否是pdf

作者: 蝼蚁撼树 | 来源:发表于2018-01-30 13:36 被阅读0次
+ (BOOL)isPDF:(NSString *)filePath
{
    BOOL state = NO;

    if (filePath != nil) // Must have a file path
    {
        const char *path = [filePath fileSystemRepresentation];

        int fd = open(path, O_RDONLY); // Open the file

        if (fd > 0) // We have a valid file descriptor
        {
            const char sig[1024]; // File signature buffer

            ssize_t len = read(fd, (void *)&sig, sizeof(sig));

            state = (strnstr(sig, "%PDF", len) != NULL);

            close(fd); // Close the file
        }
    }

    return state;
}

c函数strnstr(char* s1, chars2, int pos1) 各个参数的含义
/
*

  • strnstr - Find the first substring in a %NUL terminated string
  • @s1: The string to be searched
  • @s2: The string to search for
  • @pos1: 在s1的前pos1字符中查找
    */

相关文章

网友评论

      本文标题:判断文件是否是pdf

      本文链接:https://www.haomeiwen.com/subject/ukqczxtx.html