istream question

This is a discussion on istream question within the C++ Programming forums, part of the General Programming Boards category; How do I pass an istream to a function so that when the function is finished the input is at ...

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

    istream question

    How do I pass an istream to a function so that when the function is finished the input is at the last character read? I have tried passing by reference...
    Code:
    void readInput(istream& input)
    {
    .
    input >> data;
    .
    }
    when it is finished input is back at the top of the file... I need it at the last character read.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by lord View Post
    How do I pass an istream to a function so that when the function is finished the input is at the last character read? I have tried passing by reference...
    Code:
    void readInput(istream& input)
    {
    .
    input >> data;
    .
    }
    when it is finished input is back at the top of the file... I need it at the last character read.
    Unless you specifically use seekg() in your code, I'd say that I don't beleive you.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    I am doing something funny, then -- I don't see it, though:

    Code:
    void readInput(istream& input, char data)
    .
    .
    .
    int main
    {
    cout<<"Enter the file name: ";
    	getline(cin, filename); 
    	ifstream input( filename.c_str() );
    	input >> data;
    	
    	while( data != '#' )
    	{
    	          readInput(input, data);
                      input >> data; cout<<data; //outputs first character of file
                      input >> data; cout<<data; //outputs first character of file
                      input >> data; cout<<data; //outputs first character of file
             }
    }
    .
    .
    .
    void readInput(istream& input, char data)
    //works as expected in this function
    {
    .
    input >> data;
    .
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Since your code doesn't compile, and there are several "..." sections, I can not test YOUR code, but this mashup of something like your code certainly works. I had to change the code around a bit, so that it wouldn't stop before it printed anything (since the first part of my program is a #, and that's where your loop stops - also, I had to change it to stop at the end of the file. I also hard-coded the input file to use the source of itself).

    Also, if you want the char read in "readInput" to be fed back to your actual program, then you need to pass it by reference.

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    void readInput(istream& input, char &data);
    
    int main()
    {
        char data;
        ifstream input("foo.cpp");
        input >> data;
        
        while( data != '/' && input)
        {
    	readInput(input, data);
    	input >> data; cout<<data; //outputs first character of file
    	input >> data; cout<<data; //outputs first character of file
    	input >> data; cout<<data; //outputs first character of file
        }
    }
    
    void readInput(istream& input, char &data)
    {
        input >> data;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    What if you add data = 'x' after the call to readInput:
    Code:
    	          readInput(input, data);
                      data = 'x';
                      input >> data; cout<<data; // does this output x?
    If x is output, that means that input doesn't read anything into data. That probably happens because input has the failbit or eofbit set.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Daved View Post
    What if you add data = 'x' after the call to readInput:
    Code:
    	          readInput(input, data);
                      data = 'x';
                      input >> data; cout<<data; // does this output x?
    If x is output, that means that input doesn't read anything into data. That probably happens because input has the failbit or eofbit set.
    Good point.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    It is outputting the x... you can explain the problem a little more?

  8. #8
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    I think I know what the problem is...

    Daved. in the called function I was reading into input >> data the last character of the file... when it returned to main I input >> data again....



    solved.
    Last edited by lord; 11-13-2008 at 03:34 PM.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by lord View Post
    It is outputting the x... you can explain the problem a little more?
    You have reached the end of the input, or perhaps you are trying to read an integer or float value from the input, and it's not successful. If so, the "fail" bit is set, and from then on, nothing else will be input.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. istream question
    By dnguyen1022 in forum C++ Programming
    Replies: 7
    Last Post: 04-15-2009, 09:30 AM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. ostream and istream
    By xddxogm3 in forum C++ Programming
    Replies: 2
    Last Post: 10-20-2003, 07:36 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

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