手始めに、これは宿題の一部です。割り当ては、FAT12 ファイルからデータを取得しています。私の問題は、コードを実行すると、関数から偽の値 (巨大な負の数) が返されることです。ただし、1 つまたは 2 つの関数を実行するだけで、正しい値が得られます。
私の関数: 私の関数は同じパターンに従いますが、fseek と fread のオフセットが異なります。
void getOSName(FILE *fp, char *osname)
{
fseek(fp,3L,SEEK_SET);
fread(osname,1,8,fp);
}
int totalSize(FILE *fp)
{
int *tmp1 = malloc(sizeof(int));
int *tmp2 = malloc(sizeof(int));
if (tmp1 == 0 || tmp2 == 0)
{
printf("Malloc of tmp in function 'totalSize' failed\n");
exit(0);
}
int retVal;
fseek(fp,0xb,SEEK_SET);
fread(tmp1,1,2,fp);
fseek(fp,0x13,SEEK_SET);
fread(tmp2,1,2,fp);
if (tmp2 == 0)
{
// pverflow, value is larger than 65535 blocks
//looking in offset
printf("In tmp2 IF\n");
fseek(fp,0x20,SEEK_SET);
fread(tmp2,1,2,fp);
}
retVal = *tmp1 * (*tmp2);
free(tmp1);
free(tmp2);
return retVal;
}
int NumberFAT(FILE *fp)
{
int *tmp1 = malloc(sizeof(int));
if (tmp1 == 0)
{
printf("Malloc of tmp in function 'NumberFAT' failed\n");
exit(0);
}
int retVal;
fseek(fp,16L,SEEK_SET);
fread(tmp1,1,1,fp);
retVal = *tmp1;
free(tmp1);
return retVal;
}
主な機能
int main(int argc, char *argv[])
{
FILE *fp = FileIn(argc,argv,"r");
char *osname = malloc(sizeof(char)*8);
if (osname == 0 )
{
printf("Malloc Failed\n");
exit(0);
}
int size,size2, Filenumb, FATnumb, FATsec;
getOSName(fp,osname);
printf("OS Name: %s\n", osname);
size = totalSize(fp);
printf("Total size of disk: %d\n", size); //always returns correct value
size2 = freeSpace(fp);
printf("Free size of the disk: %d :: %d\n",size-size2,size2); //Value is wrong due to incorrect logic
printf("==============\n");
Filenumb=numberFiles(fp);
printf("The number of files in the root directory (not including subdirectories): %d\n",Filenumb); //should get 3, get 224 by itself, 2144599840 with other functions
printf("\n==============\n");
FATsec=NumberFAT(fp);
printf("Number of FAT copies: %d\n",FATsec); //should and do get 2 by itself, -214400062 with other functions
FATnumb=Sectors(fp);
printf("Sectors per FAT: %d\n",FATnumb); // should and do get 9 by itself, -21400055 with other functions
free(osname);
fclose(fp);
return 0;
}
私の問題はポインターの問題であると言いたいのですが、どこでどのように見られないだけです。私の結果がどのように、そしてなぜ不安定なのかについての洞察は大歓迎です。