RagHaven
RagHaven

Reputation: 4347

Using fseek and fread

I am working on a project that reads data from bin files and processes the data. The bin file is huge and is about 150MB. I am trying to use fseek to skip unwanted processing of data.

I am wondering if the processing time of fseek is the same as fread.

Thanks!

Upvotes: 4

Views: 3150

Answers (4)

Angelom
Angelom

Reputation: 2531

If you are processing huge files have you considered alternatives to read/write? You may find that mmap() (UNIX) or MapViewOfFile (Windows) is a more suitable alternative. The following UNIX example demonstrates opening a file for reading and counting the occurance of the ASCII character 'Q'. NOTE - all error checking has been omitted to make the example shorter.

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>


int main(int argc, char **argv)
{
  int i, fd, len, total;
  char *map, *ptr;

  fd = open("/tmp/mybigfile", O_RDONLY);

  len = lseek(fd, SEEK_END, 0);

  map = (char *)mmap(0, len, PROT_READ, MAP_SHARED, fd, 0);

  total = 0;
  for (i=0; i<len; i++) {
    if (map[i] == 'Q') total++;
  }

  printf("Found %d instances of 'Q'\n");

  munmap(map, len);

  close(fd);
}

Upvotes: 0

I probably feel fseek might be bit faster than fread as fseek changes the pointer position to the new address space that you have mentioned and there is no date read is happening.

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 882806

I am wondering if the processing time of fseek is the same as fread.

Probably not though, of course, it's implementation-dependent.

Most likely, fseek will only set an in-memory "file pointer" without going out to the disk to read any information. fread, on the other hand, will read information.

An fseek to file position 149M followed by a 1M fread will probably be faster than 150 different 1M fread calls, throwing away all but the last.

Upvotes: 0

Pavan Manjunath
Pavan Manjunath

Reputation: 28605

fseek just repositions the internal file pointer whereas fread actually reads data. So I guess fseek should be much faster than fread

If you are really curious to see what's happening behind the screen, download glibc from here and check for yourself :)

Upvotes: 3

Related Questions