fread() on Win98???

Started by
13 comments, last by MrSandman666 23 years, 4 months ago
Do not use the _cnt variable directly! Do not not read it''s value nor change it''s value!
Advertisement
ok. well, your code looks good to me. i guess there are two options from here. one is to load the entire file into a temporary buffer and read data from the buffer. or meddle with the FILE structure. i suggest the first, actually the first will make your routine a lot faster {if you ask me}. have you checked the error result from ferror(FILE *);?
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
ferror() return 32. Whatever that means. I already searched MSDN but I didn''t seem to be able to find a table with the meaniings of these error codes. A plain ''32'' doesn''t help me at all. I already knew that there was an error.

And how would you read a file into a buffer and then extract info from that buffer? I mean, my file is a big bunch of variables of all kinds of data types and I don''t even know how big the file is. I have no clue how to read that into ONE buffer and then extract the information out of it later. And why would it speed up my routine? I have to read the file anyway. One way or the other. Whether I process the information right away or later wouldn''t really make a difference, would it?

"Mr Sandman bring me a dream"
-----------------------------"Mr Sandman bring me a dream"
firstly, reading the entire file once into a buffer will definitely speed up the process because a: you don''t have function call overhead of calling "fread" so many times. b: the drive doesn''t need to be accessed so many times. and c: memory is alway faster then drive access { that''s why we have RAM, or we''d just use hard drives as RAM ( not meaning to be rude ) }. now to get the file size just use the following code.

long filesize(FILE *fHandle){  long lPrevPos, lResult;  lPrevPos = ftell(fHandle);  fseek(fHandle, 0, SEEK_END);  /* the position of the end of the file is the file size. */  lResult = ftell(fHandle);  /* return to the previous file pointer position. */  fseek(fHandle, fPrevPos, SEEK_SET);  return (lResult);} 


i can''t tell you why the code isn''t working. contact MS for answer. i''ve found some bugs in MSVC++ in all sorts of wierd areas. i just work around them.

now if you want to access data from within the memory buffer i''ll show you an example.

typedef struct {  char id[10];  int  size;  long ptr_to_chunk;} FILEHEADER;typedef struct {/* whatever you want. */} CHUNK;/* function. */void loadfunc(void){  /* temporary variables. */  long       lSize;  void       *pBuf;  FILEHEADER *pHeader;  CHUNK      *pChunk;  FILE       *fHandle;  /* attempt to open the file. */  if ((fHandle = fopen("somefile.dat", "rb") != NULL) {    /* using "filesize" function from above. */    if ((lSize = filesize(fHandle)) < sizeof(FILEHEADER)) {      /* attempt to allocate temporary buffer. */      if ((pBuf = malloc(lSize)) != NULL) {        /* attempt to read bytes. */        if (fread(pBuf, 1, lSize, fHandle) != lSize) {          /* validate file header. */          pHeader = (FILEHEADER *)pBuf;          if (strncmp(pHeader->id, "myid", 4) == 0) {            if (pHeader->size == lSize) {              if (pHeader->ptr_to_chunk > lSize) {                pChunk = ((char *)pBuf + pHeader->ptr_to_chunk);                /* validate the chunk: etc, etc, etc. */              }              else {                /* bad chunk pointer. */                free(pBuf);                fclose(fHandle);                return (0);              }            }            else {              /* invalid size. */              free(pBuf);              fclose(fHandle);              return (0);            }          }          else {            /* invalid header id. */            free(pBuf);            fclose(fHandle);            return (0);          }        }        else {          /* unable to read bytes. */          free(pBuf);          fclose(fHandle);          return (0);        }      }      else {        /* could not allocate buffer. */        fclose(fHandle);        return (0);      }    }    else {      /* invalid file: file smaller than header. */      fclose(fHandle);      return (0);    }  }  /* could not open file. */  else return (0);}profile the code yourself. i guarantee this will be faster than file access. again, i don''t know why reading a file a bunch of times didn''t work. this is just an alternate solution.    
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
WOOOOOHOOOOOO!
I fixed it! YEEEEHAAAAAAAAA!

The problem was this line (and two others similar to that):
fread(Joints.keyFramesRot, sizeof(keyframerot), Joints.numKeyFramesRot, File);<br><br>changing it to:<br>for(x = 0; x < Joints.numKeyFramesRot; x++)<br> fread(Joints.keyFramesRot, sizeof(keyframerot), 1, File);<br><br>fixed the problem.<br><br>It was so simple and I didn''t even try it! SOmetimes I could hit myself but that has to wait. At the moment I''m busy celebrating my success!<br><br>See ya and many thanx to y''all! </i> <br><br> "Mr Sandman bring me a dream"
-----------------------------"Mr Sandman bring me a dream"

This topic is closed to new replies.

Advertisement