ifstream::peek() cost

This is a discussion on ifstream::peek() cost within the C++ Programming forums, part of the General Programming Boards category; Just a quick question, would it be relatively expensive to call peek() for every byte read in, to check for ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,530
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 08:26 PM

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