我已经为此进行了广泛搜索,现在花了4个小时,我希望有人能帮助我。Windows/TCC/C读取随机跳转的二进制文件指针
我有一个简单的程序来读取一个二进制文件,大约2.7 MB。该程序使用tcc编译器在Windows上编译。我在各种高级语言(Pascal,Modula2,Matlab,PHP,Basic)方面经验丰富,但对C来说是新手,并且怀疑这对于内存分配和变量被覆盖有一些影响。
void main()
{
long int start_loc;
long int actual_loc;
int seek_result;
char file_to_process[]="d:/tmp/pfc/test.out";
char read_int;
int spaces;
int read_result;
FILE *data_file;
//fpos_t position;
data_file=fopen(file_to_process,"r");
if (data_file == NULL) {
printf("Error");
}
start_loc = 1002;
printf("\n size of start_loc : %d",sizeof(start_loc));
actual_loc = ftell(data_file);
printf("\nBEFORE location %d \n",actual_loc);
seek_result = fseek(data_file, start_loc, SEEK_SET); //move to start of search location in the file
actual_loc = ftell(data_file);
printf("\n AFTER seek location %d \n",actual_loc);
fread(&read_int, 1, 1, data_file);
actual_loc = ftell(data_file);
printf("\n AFTER read location %d \n",actual_loc);
printf("\n read result %x" ,*&read_int);
fread(&read_int, 1, 1, data_file);
actual_loc = ftell(data_file);
printf("\n AFTER read location %d \n",actual_loc);
printf("\n read result %x" ,*&read_int);
fclose(data_file);
return;
}
在上述我从文件中的位置1002读出的实例 - 这个工作正常 - 其结果是:
size of start_loc : 4
BEFORE location 0
AFTER seek location 1002
AFTER read location 1003
read result 0
AFTER read location 1004
read result 3
一切正常 - 由1个字符的文件指针前进每个字节读。
问题出在某个值的起始位置,例如
start_loc = 16000
在这种情况下,文件指针例如命令后跳跃一个看似随机的方式即读取1个字节并且文件指针移动到19586.
size of start_loc : 4
BEFORE location 0
AFTER seek location 16000
AFTER read location 19585
read result 47
AFTER read location 19586
read result 0
感谢您阅读这篇文章!
如果你不想说“这不是真正的随机!”的话,避免说“随机跳跃”。 – OJFord
除非我误解了你想要达到的目标,'size_of(start_loc)'并没有按照你的想法去做。这给你'start_loc'类型(long int)的内存大小。 – OJFord
是的,这是正确的 - 我很困惑发生了什么我想检查它真的是4个字节长,因此可以保持文件指针值高达2^32之后,因为你说它什么都不做 – rubusrubus