欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

判断文件是否是pdf

程序员文章站 2022-07-05 10:18:24
...
+ (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字符中查找
    */