Advantages of fseek() and fread() ?

This is a discussion on Advantages of fseek() and fread() ? within the C Programming forums, part of the General Programming Boards category; Hi, I don't understand why fseek() requires more time to access a heap binary file than fread(). Could anyone share ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    54

    Advantages of fseek() and fread() ?

    Hi,

    I don't understand why fseek() requires more time to access a heap binary file than fread(). Could anyone share some input on this?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    I'm not sure what exactly you're referring to, but I'll take a stab at it:

    Files are stored on harddisks in chunks called sectors (typically 4KB each). To fseek() deep into the file, it requires the OS to retrieve the information about where the chunk of file you're looking for is located. That can require several disk accesses. fread() (depending on how much data you're reading) can usually just get the data from memory (the read-ahead buffer).

    But really, fseek() and fread() do completely different things even though they both deal with files. It's like comparing apples and oranges.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    54
    Hmmm, because I'm comparing a sorted file and an unsorted(heap), I used fread() to read the heap file, which was faster than a sorted file. In this case, I think the best searching algorithm would be a linear search because of the data is presented in an unordered fashion.

    On the other hand, a sorted file, a searching algorithm with fseek() could prove useful and theoratically fasterl. My main concern is that I'm wondering why my linear search on a heap is faster than a sorted file with a proper searching algorithm.

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Memory is free, so why not do bulk reads from the file then search in memory? If you have a buffer large enough to store the entire file, you could call fread() one time to get the file, then you could preform a number of sorting/searching (canned) calls to find your data -- Assuming that you are able to read the data from the disc as fast as you say you can.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You could use an insertion sort if you're concerned about memory usage. You don't need two copies of your data at the same time in memory.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    54
    Memory is free, so why not do bulk reads from the file then search in memory? If you have a buffer large enough to store the entire file, you could call fread() one time to get the file, then you could preform a number of sorting/searching (canned) calls to find your data -- Assuming that you are able to read the data from the disc as fast as you say you can.
    Yes. I could do that, which saves alot of time. I'm just wondering why is my linear searching algorithm on heap (I used only fread() to read certain chunks of data) is faster than a searching algorithm on a sorted file (I used a Binary Search with fseek() to locate the data).

    As far as I know, a sorted file as input is always faster than an unsorted file.
    Last edited by DarrenY; 04-03-2007 at 12:14 PM.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Yes. I could do that, which saves alot of time. I'm just wondering why is my linear searching algorithm on heap (I used only fread() to read certain chunks of data) is faster than a searching algorithm on a sorted file (I used a Binary Search with fseek() to locate the data).

    As far as I know, a sorted file as input is always faster than an unsorted file.
    It's because file I/O is so slow. It's so slow that a linear search in memory is faster than a binary search on the disk. That's why, unless your file is truly gigantic, you probably want to read it into memory and do a search on it there.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    54
    Thanks for the input guys! I think it enlightened me a little about the time cost of fseek() and fread().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fseek failing - Error: Invalid argument
    By avi2886 in forum C Programming
    Replies: 14
    Last Post: 05-14-2009, 08:49 PM
  2. fread problems or memory problems
    By Lechuza in forum C Programming
    Replies: 1
    Last Post: 03-22-2009, 12:45 PM
  3. fread & fseek not advancing buffer
    By RobotGymnast in forum C++ Programming
    Replies: 6
    Last Post: 10-28-2008, 04:38 PM
  4. Image Processing Help Required
    By dantu1985 in forum C Programming
    Replies: 7
    Last Post: 12-31-2007, 08:30 AM
  5. problem with fread
    By GaPe in forum C Programming
    Replies: 3
    Last Post: 02-23-2003, 02:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21