| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
ifstream in function
Hi all. I'm a student learning C++.
I have worked through all of my problems on my latest project except for one. I have to use a fuction to get input from a file. I make this call: getInput (month, rainfall); and the function is this: 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); in_stream >> rmonth >> rrainfall; //in_stream.close(); } (pardon all of the comments, I was trying a few different things) What happens is that the file is reopened everytime and data starts right from the beginning again. I tried appending data but that didn't do it. I tried not re-opening but If I don't declare the file name in the function, in_stream is unidentified. Yes, I'm very new and this is probably a fairly benign problem. Thanks in advance. |
|
#2
|
||||
|
||||
|
hm, assuming you had to leave that function parameter as is and could change what arguements it's allowed to take, I'd recommend the following:
Use a file pointer to open the file, but make the file pointer a global variable. Then just use the file pointer in the function. That way, whenever the function exits, it's doesn't close the file pointer.
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips. ![]() |
|
#3
|
|||
|
|||
|
Quote:
Thanks guys for the help. I'll give it a try. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > ifstream in function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|