Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to use fread/fseek
#1
First of all I would like to commend you on your fine program. I am having a problem in certain instances when trying to do the following code and I was wondering if you recommended a different way of doing what I'm trying to do:
open a file, fseek to a spot in the file, read 4 bytes into an integer, and then call ntohl because the integer in the file is in network order

I am able to do this in 99% of the files I read, but in this one file it just won't work and I can't figure it out
Is there something different I need to be doing as far as passing in the buffer/defining the c function for fread, because from what I have seen so far it appears like fread is not working right for whatever reason (overflow or what, I'm not sure).

The value I should be getting from the fread is 447,546,112 (base 10) and then ntohl should make it 240,922, but I am getting 11,338,496 (base 10) and then ntohl makes it 240,896 (which ntohl works ok, but its working on the wrong value!). Please let me know if you can help or if you need more information to understand my problem.
thanks,

,,dll msvcrt #fopen $fname $mode
,,dll msvcrt #fseek int*fptr addr mode
,,dll msvcrt #fread byte*buf size cnt int*fptr
,,dll msvcrt #fclose int*fptr
,,dll ws2_32 #ntohl ntohlVar
,,
,,int* inFile = +fopen(fname, "r")
,,fseek(inFile, 96, 0);
,,fread(&tempInt, 1, 4, inFile);
,,fclose(inFile);
,, mes(tempInt "" "")
,,magNum = ntohl(tempInt);
,,mes(magNum "" "")
#2
Use "rb" mode. That is, binary. By default, file is opened in text mode, where some characters are translated (eg \n to \r\n).

,,int* inFile = +fopen(fname, "rb")

You can also use File class. It wraps fopen and similar functions. Example:

Code:
Copy      Help
dll ws2_32 #ntohl ntohlVar

File f.Open(fname "r") ;;Open appends b
f.SetPos(96)
f.Read(&tempInt 1 4)
;mes(tempInt "" "")
magNum = ntohl(tempInt);
mes(magNum "" "")
#3
Thank you, for some reason this didn't hit me.

Thanks again for your great program!


Forum Jump:


Users browsing this thread: 1 Guest(s)