Search code examples
cstructfreadfseek

Why isn't my Struct char member being assigned the value 'F' and instead is staying the default initialization value?


I am reading in binary data from a disk image and when buffer[0] is equal to 3, the RootDirectory[j] should have its status member be 'F'. However when i run the code the value is 0. The if statement (chartest == 3) is entered every time it should be entered though.

typedef struct{
    char Status;
    int StartingBlock; 
    int BlockNumbers; 
    int FileSize; 
    int CreateTime;
    int ModifyTime; 
    char FileName[31]; 
}RootDirectory; 

RootDirectory *Dir =  (RootDirectory*)malloc(sizeof(RootDirectory)* RootNum);
char buffer1[50]; 
int chartest; 
int j = 0; 
int RootBytes = Info.RootDirectoryBlocks * Info.BlockSize; 
int DirectoryEntries = RootBytes/64;
for(int i = 0; i < DirectoryEntries; i++){
    int statusread = fread(buffer1, 1,1, fp); 
    if((int) buffer1[0] == 0){
        fseek(fp, 63, SEEK_CUR); 
    }
    else{
        chartest = (int)buffer1[0]; 
        if(chartest == 3){
        Dir[j].Status = 'F'; 
        }
        fseek(fp, 63, SEEK_CUR); 
        j++; 
    }
}
for(int i = 0; i < j; i++){
    printf("%c\n", Dir[j].Status); 
}

Solution

  • It sould be printf("%c\n", Dir[i].Status); in the last loop. Not Dir[j].Status