[英]C program reading from a file incorrectly
我有以下用c編寫的代碼。 它會打開一個JPEG文件,並根據文件的某些位置從中獲取一些數據。 輸出對“制造商”和“模型”正確,但對其他任何東西都不正確。 在分配中,當tagIdentifier等於0x8827時,它需要轉到文件中由tiff.valueofDataItem的值定位的位置。
從作業中得出:“如果遇到0x8769標識符,則文件中的其他地方還有一個額外的Exif塊。即使沒有讀取所有計數標簽,我們也可以在此時停止讀取,因為TIFF格式指出所有標識符必須是我們將在此Exif子塊標簽中查找特定的oset,再一次+12個字節。在此,再重復一次以上過程,以獲得有關圖片的更多特定信息。首先,讀入一個新的計數為無符號的短。接下來,循環,從文件中讀取更多12字節的TIFF標簽。”
它輸出正確的制造商和模型,但其他任何方法均無效。 是從文件中的錯誤位置讀取還是以int類型讀取錯誤的問題。 另外,它表示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;
}
您的代碼有幾個問題。 如前所述,您假設EXIF標記的位置。
此外,您假設結構的布局。 編譯器可以對齊成員,將所有內容丟掉。
您也不會考慮字節順序。 如果您在低端字節序處理器上運行,則您的代碼將無法工作。
您需要讀取字節數組。 對於整數,需要將指針強制轉換為特定的數組元素,並且如果需要,需要更正字節順序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.