C程序错误地从文件中读取

时间:2014-02-13 03:55:11

标签: c jpeg binaryfiles readfile exif

我有以下用c编写的代码。它会打开一个JPEG文件,并根据文件的某些位置从中获取一些数据。输出对“制造商”和“模型”是正确的,但对其他任何内容都没有。在赋值中,当tagIdentifier等于0x8827时,它需要转到由tiff.valueofDataItem的值定位的文件中的某个点。

从作业: “如果我们遇到0x8769标识符,则文件中的其他地方还有一个额外的Exif块。即使我们没有读取所有计数标记,我们也可以停止读取,因为TIFF格式表明所有标识符必须按排序顺序排列。 我们将在此Exif子块标记中寻找特定的oset,再次+12字节。在那里,再一次重复上述过程,以获得有关图片的更具体信息。 首先,将新计数读作无符号短。接下来,循环,从文件中读取更多12字节的TIFF标记。“

它输出正确的制造商和型号,但没有其他工作。这是从文件中的错误位置读取还是读取错误的问题。此外,它表示0x829A,0x829D,0x920A是(2个32位无符号整数的分数)。它说它们“的行为就像我们用字符串做的那样,而不是读取几个单字节字符,读取2个无符号整数。我也不知道如何。

  #include <stdio.h>
  #include <string.h>
  #include <stdlib.h>

  struct EXIF{
    unsigned short startOfFile;
    unsigned short JPEGAPP1marker;
    unsigned short lengthOfAPP1block;
    unsigned char exitString[4];
    unsigned short nullTerminator;
    unsigned char endianness[2];
    unsigned short versionNumber;
    unsigned int offsettoTIFFblock;

 };

  struct TIFF{
    unsigned short tagIdentifier;
    unsigned short dataType;
    unsigned int numOfDataItems;
    unsigned int valueOfDataItem;

  };

   int main(int argc, char *argv[]){

    int i;
    FILE *fp;
    fp = fopen(argv[1], "rb+");

    if(fp == NULL){
            perror("ERROR!");
            exit(1);
    }

    struct EXIF exif;
      int x = fread(&exif, sizeof(struct EXIF), 1, fp);

    unsigned short count;
    int y = fread(&count, sizeof(short), 1, fp);

    struct TIFF tiff;
    int location = 22;
    char manufacturer[15];
    char cameraModel[50];
    unsigned int exifBlock;

    for(i = 0; i < count; i++){
            fread(&tiff, sizeof(struct TIFF), 1, fp);
            if(tiff.tagIdentifier == 0x010F){
                    fseek(fp, tiff.valueOfDataItem + 12, SEEK_SET);
                    fread(manufacturer, sizeof(char), tiff.numOfDataItems, f$
            }
            if(tiff.tagIdentifier == 0x0110){
                fseek(fp, tiff.valueOfDataItem + 12, SEEK_SET);
                 fread(cameraModel, sizeof(char), tiff.numOfDataItems, fp$

            }
            if(tiff.tagIdentifier == 0x8769){
                    fseek(fp, tiff.valueOfDataItem + 12, SEEK_SET);
                    location = tiff.valueOfDataItem + 12;
                    break;

            }
            location = location + 12;
            fseek(fp, location, SEEK_SET);
    }

    unsigned short newCount;
    int z = fread(&newCount, sizeof(short), 1, fp);
    int j;
    int width;
    int height;
    int ISOspeed;
    char dateTaken[2];

    for(j = 0; j < newCount; j++){
            fread(&tiff, sizeof(struct TIFF), 1, fp);
            if(tiff.tagIdentifier == 0xA002){
                    fseek(fp, tiff.valueOfDataItem + 12, SEEK_SET);
                    fread(&width, sizeof(int), tiff.numOfDataItems, fp);

            }
            if(tiff.tagIdentifier == 0xA003){
                    fseek(fp, tiff.valueOfDataItem + 12, SEEK_SET);
                    fread(&height, sizeof(int), tiff.numOfDataItems, fp);

            }
            if(tiff.tagIdentifier == 0x8827){
                    fseek(fp, tiff.valueOfDataItem + 12, SEEK_SET);
                    location = tiff.valueOfDataItem + 12;
                    break;

            }
            location = location + 12;
            fseek(fp, location, SEEK_SET);
    }
    unsigned short newCount;
    int z = fread(&newCount, sizeof(short), 1, fp);
    int j;
    int width;
    int height;
    int ISOspeed;
    char dateTaken[2];
    int exposureTime;
    int focalLength;
    int fstop;


    for(j = 0; j < newCount; j++){
            fread(&tiff, sizeof(struct TIFF), 1, fp);
            if(tiff.tagIdentifier == 0xA002){
                    fseek(fp, tiff.valueOfDataItem + 12, SEEK_SET);
                    fread(&width, sizeof(int), tiff.numOfDataItems, fp);

            }
            if(tiff.tagIdentifier == 0xA003){
                    fseek(fp, tiff.valueOfDataItem + 12, SEEK_SET);
                    fread(&height, sizeof(int), tiff.numOfDataItems, fp);

            }
            if(tiff.tagIdentifier == 0x8827){
                    fseek(fp, tiff.valueOfDataItem + 12, SEEK_SET);
                    fread(&ISOspeed, sizeof(int), tiff.numOfDataItems, fp);

    }if(tiff.tagIdentifier == 0x829A){
                    fseek(fp, tiff.valueOfDataItem + 12, SEEK_SET);
                    fread(&exposureTime, sizeof(int), tiff.numOfDataItems, fp);

            }if(tiff.tagIdentifier == 0x829D){
                    fseek(fp, tiff.valueOfDataItem + 12, SEEK_SET);
                    fread(&fstop, sizeof(int), tiff.numOfDataItems, fp);

            }
            if(tiff.tagIdentifier == 0x920A){
                    fseek(fp, tiff.valueOfDataItem + 12, SEEK_SET);
                    fread(&focalLength, sizeof(int), tiff.numOfDataItems, fp);

            }
            if(tiff.tagIdentifier == 0x9003){
                    fseek(fp, tiff.valueOfDataItem + 12, SEEK_SET);
                    fread(dateTaken, sizeof(char), tiff.numOfDataItems, fp);
            }
             location = location + 12;
            fseek(fp, location, SEEK_SET);
    }


    printf("Manufacturer: %s\n", manufacturer);
    printf("Model: %s\n", cameraModel);
    printf("Width: %d pixels\n", width);
    printf("Height: %d pixels\n", height);
    printf("Date taken: %s\n", dateTaken);



    fclose(fp);
    return 0;
}

1 个答案:

答案 0 :(得分:0)

您的代码存在一些问题。如前所述,您假设EXIF标记的位置。

另外,您假设结构的布局。编译器可以调整成员关闭所有内容。

您也没有考虑字节顺序。如果您使用的是小端处理器,则代码将无法正常工作。

您需要读取字节数组。对于整数,您需要将指针转换为特定的数组元素,如果需要,您需要更正字节顺序。