判断文件是否是pdf

2018-01-30  本文已影响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) 各个参数的含义
/
*

上一篇 下一篇

猜你喜欢

热点阅读