Using peek()

This is a discussion on Using peek() within the C++ Programming forums, part of the General Programming Boards category; ive read up on peek()...have a few questions regarding it... 1) does it check for a character or a word? ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    124

    Using peek()

    ive read up on peek()...have a few questions regarding it...
    1) does it check for a character or a word?
    2) does it check for space? i know it checks if there is a newline next in the stream...

    the reason i'm asking is because of my earlier tree question...if the file im reading is something like this:
    Code:
    pat = none : NO
    pat = some : YES
    pat = full : 
       hun = yes : NO
    above is a tree structure...where pat and hun are treenodes, none, some, full and yes are branches and NO, YES are classes these branches end up into...in the 3rd line the branch full does not end up into a class but connects to another node which is hun...

    now in my tree reading algorithm the 3rd line is the problem...there is a spaces after : in each line...

    I want to have a check which tells me if there is a word after the colon or if there is noword there on that line...based on that i do two different algorithms...so would this work:
    Code:
    ifstream in(filename);
    in >> word; //word is a string and assume i just read in : into word
    if (in.peek() != '\n')
    {
       //does this mean that i have either YES or NO here?
    }
    if (in.peek() == '\n')
    {
       //does this mean that if i do in >> word now i'll have the value hun in word?
    }
    Regards,

    Farooq

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Streams only work with characters, and peek is an unformatted operation. That means that it simply gives you the next character on the stream, whatever it is, but doesn't remove it.

    >so would this work
    The best way to figure these things out is to try them. You now know how peek works, so a test should serve you well.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    actually ive used peek () in some code before...and although it worked...the slightest change in the file e.g space before newline character on each line messed things up...

    so i'm using the getline now...and then i use a string parser to extract words from the string...this seems more robust...and i find it's easier to implement as well...orignally i was supposed to use this method for another file but i've figured out that this would also be best for the given tree structure...

    Regards,

    Farooq

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input stream question
    By lord in forum C++ Programming
    Replies: 4
    Last Post: 09-11-2008, 09:27 PM
  2. Filestreams - opening them and peek()
    By Crystylla in forum C++ Programming
    Replies: 2
    Last Post: 03-25-2006, 02:19 PM
  3. sneak peek of next line in file
    By mbooka in forum C Programming
    Replies: 2
    Last Post: 02-16-2006, 12:07 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Iterative Tree Traversal using a stack
    By BigDaddyDrew in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2003, 05:44 PM

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