Thread: input stream question

  1. #1
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61

    input stream question

    Is there anyway to read a character from a string array into a char variable but not have the "marker position" in the input stream move?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Can you rephrase the question? You are reading from a string array into the char variable, what does an input stream have to do with it?

    You can assign a character in a C++ string or C-style string to a char variable directly (using operator[] to access the character).

    If you're reading from an input stream and trying to read a character without extracting it from the stream, then perhaps peek() would work?

  3. #3
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    Yes, your last line is what I am trying to do. Peek() seems like it would work but is there anyway I can use it but have it skip newlines that you know of?

    Right now I am using ignore then peek but perhaps there is a cleaner way...

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    peek() can only peek at the next character, so if you want to peek at the character after the newline, then you'd have to ignore() it first. That shouldn't be too difficult, though, unless for some reason you wanted to leave the newline in the stream and peek at the character after it.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    unless for some reason you wanted to leave the newline in the stream and peek at the character after it.
    Perhaps you could use putback(). Extract the newline from the streem to peek the following character, and then put the newline back with putback.

    Not sure how scalable that is. Think of a blank line, and IIRC, you are only guaranteed one character of putback.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  3. Input stream directly to structure elements?
    By thenrkst in forum C++ Programming
    Replies: 1
    Last Post: 04-24-2003, 11:43 AM
  4. clearing input stream
    By Sub in forum C++ Programming
    Replies: 2
    Last Post: 01-21-2002, 08:59 PM