Thread: ifstream::peek() cost

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    ifstream::peek() cost

    Just a quick question, would it be relatively expensive to call peek() for every byte read in, to check for an upcoming EOF()? Because I need to know a byte in advance. Or should I just find the file size and do a test to see if the number of bytes read has reached that number?

    I'm asking because I'm writing a Huffman/other compression codec module for potential use in future programs, and not all bits in the last byte of the file may be used.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > would it be relatively expensive to call peek() for every byte read in,
    That would depend on the quality of your implementation.

    Read the file a block at a time into a buffer, then you'll know well in advance where the real EOF is before your code gets to it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ok, thanks.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what's the hidden cost of creating object?
    By kypronite in forum C++ Programming
    Replies: 13
    Last Post: 10-12-2008, 04:41 PM
  2. Cost of programming
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 05-28-2008, 09:36 AM
  3. structural fault any ideas
    By kaijuu in forum C Programming
    Replies: 17
    Last Post: 04-17-2007, 02:43 PM
  4. Binary Search Tree Cost of Path
    By indi_kila in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2006, 08:20 PM
  5. cost of living on your own?
    By Leeman_s in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 01-10-2004, 09:26 PM