判断文件是否是pdf

+ (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字符中查找
    */
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容