ifstream in a function

This is a discussion on ifstream in a function within the C++ Programming forums, part of the General Programming Boards category; Hello. I am just finishing a project and I have gotten passed all of the other bugs I've had without ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    11

    ifstream in a function

    Hello.

    I am just finishing a project and I have gotten passed all of the other bugs I've had without help from others but this is my last issue and it's driving me nuts!

    I have a program that I'm taking in data from ifstream and outputting ofstream. It takes in a int for month and an int for rainfall.

    I need a function called getInput to simply bring in 2 pieces of data at a time and then pass back to main. If I put this statement in main, the program works fine:

    Code:
    in_stream  >> month >> rainfall;
    But if I use that in a function, the data is restarted from the beginning each time.

    Here is the function i'm using:

    Code:
    void getInput (int &rmonth, int &rrainfall)
    	{
    		//::input(ifstream& inData)
    		//ifstream in_stream("rainInput.txt", ios::cur, ios::in);
    			//in_stream.open ("rainInput.txt", ifstream::in);
    				fstream in_stream;
    				in_stream.open("rainInput.txt");
    				in_stream  >> rmonth >> rrainfall;
    			//in_stream.close();
    			//in_stream.clear();
    	}
    I also appoligize for all of the comments but I have been trying many different things.

    How can I have the function take in data from the last that was taken in?

    It's probably very simple but I'm reaching my frustration limit with streams.

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Declare and open your ifstream in main, then pass it by reference to your function.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    Thanks for the response.

    Are there any examples of referencing to a ifstream object? I've have tried several things, all with errors.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    It works the same as what you have with the ints above. What did you try? What errors did you get? Make sure to post the function call in addition to the function code.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    Daved,

    Thanks again for the help. You're really helping take the weight from this 2000 boulder on my back.

    Here is the call:

    Code:
    getInput (istream & in_stream, month, rainfall);
    and here is the function:

    Code:
    void getInput (istream & in_stream, int &rmonth, int &rrainfall)
    	{
    				in_stream  >> rmonth >> rrainfall;
    	}
    I get the error: 'std::istream' : illegal use of this type as an expression

    I have been changing the code and getting assorted errors but this is the latest.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    Daved,

    I did it! Thank you so much!

    Here's what worked:

    The call:

    Code:
    getInput (in_stream, month, rainfall);

    The function:

    Code:
    void getInput (std::istream & rin_stream, int &rmonth, int &rrainfall)
    	{
    				rin_stream  >> rmonth >> rrainfall;
    	}
    and the prototype:
    Code:
    void getInput (std::istream &in_stream, int &month, int &rainfall);
    I have spent hours just on this one issue. Even if I end up failing the class, it's these small victories that make it worth the trouble.

    If you see anything that could be optimized or doesn't make sense, please let me know.

    Thanks again.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Looks good to me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 10:08 AM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM

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