// testForPDF.cpp : Check whether file is a PDF file
// returns 2 for error, 1 for "file is not PDF" and 0 for "file IS PDF"
//
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
int main(int argc, char* argv[])
{
FILE * fp;
char buff[100];
int count;
if (argc < 2 || argc > 2) {
printf ("Usage: testForPDF filename\n");
return 2;
}
fp = fopen(argv[1], "r");
if (fp == (FILE *)NULL) {
printf ("Failed to open file %s\n", argv[1]);
return 2;
}
count = fread(buff, 1, 5, fp);
buff[5] = (char)0;
if (count < 5) {
printf ("Failed to read 5 bytes from %s\n", argv[1]);
}
if (strcmp(buff, "%PDF-")) {
/* printf ("File is NOT a PDF file - header is \"%s\"\n", buff); */
return 1;
}
else {
/* printf ("File is PDF - header is \"%s\"\n", buff); */
return 0;
}
fclose (fp);
return 0;
}
|