-
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
-
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.
-
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