passing ifstream by reference doesn't work

Hey,

I'm trying to write a short program with a function ('OpenFil') that prompts the user for the name of a text file and then opens this using ifstream (passing it by reference). Then I call this function ('OpenFil') in the main in order to read in values from the text file in the main function. I know the file gets opened in the 'OpenFil' by it's not open in the main function. Can you please explain why this is so and help me to use the textfile in the main?

I can get it to work by simply including everything in the main function and not introducing the separate 'OpenFil' but I want to use this to builder a bigger program and so want to keep them separate. I also know the textfile I'm trying with is in the right directory. Thanks! My code is below:

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
using namespace std;

string GetLine();
void OpenFil(ifstream &input);

int main(){
ifstream input;
OpenFil(input);
if(input.is_open())
cout<<"The file is open"<<endl;
else
cout<<"It's not open"<<endl;//this is what i get but don't want
system("PAUSE");
return 0;
}
void OpenFil(ifstream &input){//This is the function opening a textfile specified by the user
while(true){
cout<<"Enter a file name:"<<endl;
string FileName = GetLine();
ifstream input(FileName.c_str());//I have checked and the file IS open here definitely
if(input.is_open()) return;
cout<<"Sorry that name was not valid. Please try again."<<endl;
input.clear();
}
}

string GetLine(){
string result;
getline(cin,result);
return result;
cout<<result<<endl;
}
First of all, please indent your code, it's a mess.

Second you should read tutorials on both functions and file handling, both are available here under the documentation section.

Also read on variables scope:

http://www.functionx.com/cppcli/variables/Lesson22.htm

EDIT: If that doesn't cut it, come back again :)


Last edited on
i'll try to explain;

1. you can't pass some ifstream to a function and open a file with the ifstream variables (even with pass by reference) in the function

2. you re-declare the "input" within the function, as far as i know, error!

3. GetLine()'s cout << result << endl; is never being executed...

4. CMIIW
closed account (zb0S216C)
I've noticed a few issues here.

1) You create another instance of ifstream with the same identifier within OpenFil( ).
2) In OpenFil( ), you checked if the file was open (if( input.is_open( ) )). This is fine. However, is_open( ) returns a non-zero numerical value if the file was opened successfully. Your code, however, returns to the caller if the files opens. When the function reaches a return statement, the function ends.
3) In GetLine( ) you return to the caller before the cout statement is reached.
4) You've used system( ).
5) You've included <vector> which you don't use.
6) You've included <iomanip> which you don't use.
7) You've included <sstream> which you don't use.

Wazzak
Last edited on
Thanks! I didn't realize about not being able to use ifstream to open the function if I'd already passed the ifstream to the function. I changed the code to open the file to input.open and now it works!
Topic archived. No new replies allowed.