Open text file in a function

Hi, i can't seem to figure this out, how can i open my text file when i call it in a function? The input parameter is a fstream but i'm not sure what to put in there. Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;


void allLines(fstream&);

int main ()
{
  allLines(;

  return 0;
}


void allLines (fstream&)
{
  string line;
  ifstream myfile ("Presidents.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
      cout << line << '\n';
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 
}
That depends on what you want your program to do...maybe you would give a more specific question.
Well that's what my question is, how do i open it in a function? if i put it in main it worked but i need it in a function and i haven't figured out how to do that.
Ok never mind i got it. here's the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int main ()
{
	
	fstream myfile ("Presidents.txt");


  allLines(myfile);

  return 0;
}


 void allLines (fstream&)
{
  string line;
  ifstream myfile ("Presidents.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
      cout << line << '\n';
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 
} 
Actually Im also trying to learn the fstream.h file(how to use it....
I will give a working example of a function that accepts a filename as argument....
In the mean while try to see my post titled "How to give a user specified name to a file" or something like that....you will get some help,but Im still a noob (I mean a newbie :P ).
Just wait for 5 mins!!!
Ok,idk what you actually wanted to do,but heres the program I wrote for you...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;


void allLines(char x[50]);

int main ()
{
  char name[50];
  for(int i=0;name[i]!='\0';i++)
  cin>>name;
  allLines(name);
  return 0;
}


void allLines (char x[50])//Accept a name-of-file char as argument
{
  char a[50];//Idk if accepting a string as a parameter in any function of fstream works
  char c='y';
  ifstream myfile;//pass the char to open a file
 myfile.open(x,ios::in|ios::binary);/**This is the open function,which is more efficient than the one you tried
                                     * First the name of the file is passed as argument
                                     * Then ios is a header which is the base of fstream and iostream
                                     * ios::in means taking in the input
                                     * ios::binary means saving the input in binary(.dat)mode(text is default)**/
while (c=='y'||c=='Y')
{
cout<<"Enter the thing"<<endl;
cin>>a;
myfile>>a;
cout<<"Do you wanna add more?"<<endl;
cin>>c;
if(c=='n'||c=='N')
break;
}
myfile.close();
}
Topic archived. No new replies allowed.