Open file and read from a file in Functions

Hi, I'm working on a menu where option 1 is ask user for a file name, and option 2 is read from a file.

How can I make it to functions?

I


This is what I have for option 1:
1
2
3
4
5
6
7
8
9
10
11
void inputfile();
void inputfile()
{
	ifstream masterfile;
	string filename;
	cout << "Please enter the name and extension of your file " << endl;
	cin >> filename;
        masterfile.open(filename);


}


My question is, how can I call the file name from the function inputfile so I can read the document in another function?

I have tried without functions, but when I want to call the file in case 2, it won't show any data.

Last edited on
Maybe I misunderstand, but I feel like there's some information here you're leaving out.
If your code currently doesn't have any functions, what makes you think it will magically start working once you introduce more functions? That being said, using functions is definitely recommended for organization and proper logic, so you certainly should use them!

it won't show any data.

How are you trying to make it show data? The code you posted does not make any attempt to actually read the file contents. So I'm kinda shooting in the dark as to what the actual problem is.

So you want to input the filename in inputfile(), and then actually read the document in another function, let's call it read_file_contents().

I would pass the file stream into the read function.

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


#include <iostream>
#include <string>
#include <fstream>

using namespace std;

ifstream inputfile()
{
	ifstream masterfile;
	string filename;
	cout << "Please enter the name and extension of your file " << endl;
	cin >> filename;
        masterfile.open(filename);
 	if (!masterfile)
        {
              std::cout << "warning: cannot open file" << std::endl;
        }
        
    return masterfile;
}

void read_file_contents(ifstream& file)
{
    std::string line;
    while (getline(file, line))
    {
        std::cout << line << std::endl;
    }
}

int main()
{
    ifstream file = inputfile();
    read_file_contents(file);
    
}
Last edited on
Maybe sth. like that:
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
44
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

string get_filename()
{
  string fname;

  // your code 
  cout << __FUNCTION__ << "\n";
  return fname;
}

void read_file(const string& filename)
{
  // your code 
  cout << __FUNCTION__ << "\n";
}

int main()
{
  int choice;
  string filename;

  for (;;)
  {
    cout << "\n1 - Enter filename";
    cout << "\n2 - read from file";
    cout << "\n3 - Exit";
    cout << "\nEnter your choice: ";
    cin >> choice;
    
    if (choice == 1) // maybe use switch if covered
      filename = get_filename();
    else if (choice ==2)
      read_file(filename);
    else if (choice == 3)
      break;
  }
}



CAUTION: This code is just an example of how things can be done.
It is neither the only way nor the best way.
It is not properly tested and might not be correct.
what I'm trying to do is,
case 1: ask user for file
case 2: show user's file content.

how can I user the return value of the function 1, so function 2 can read the file?

The way I modify how Ganado did, it asks again for the user input.
I thought that declaring file = inputfile(); it would take the return value of function 1.



1
2
3
4
5
6
7
8
9
10
11

void read_file_contents()
{	
	ifstream file;
	file = inputfile();
	string line;
	while (getline(file, line))
	{
		cout << line << endl;
	}
}


I also try to use the function 1 within file.open and it still asks for the name.

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

string get_filename()
{
  string fname;
  ifstream masterfile;
	cout << "Please enter the name and extension of your master file " << endl;
	cin >> fname;
	masterfile.open(fname);

	if (!masterfile)
	{
		cout << "warning: cannot open file" << endl;
	}


  
  return fname;
}

void read_file(const string& filename)
{
	ifstream file;
	file.open(get_filename());
	string line;
	while (getline(file, line))
	{
		cout << line << endl;
	}

}
Last edited on
As far as I'm aware, you can't return the fstream from a function and assign it to another variable like this:
 
    ifstream file = inputfile();

edit: it may be possible in more recent versions of C++


However, what you can do is pass the fstream by reference from one function to another. (following code based on code from earlier in this thread).

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
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;

void inputfile(ifstream& masterfile)
{
    string filename;
    cout << "Please enter the name and extension of your file " << endl;
    cin >> filename;
    masterfile.open(filename);
    if (!masterfile)
    {
        std::cout << "warning: cannot open file" << std::endl;
        exit(1);
    }
}

void read_file_contents(ifstream& file)
{
    std::string line;
    while (getline(file, line))
    {
        std::cout << line << std::endl;
    }
}

int main()
{
    ifstream file;
    
    inputfile(file);
    
    read_file_contents(file);
}


Last edited on
Thank You Chervil. I didn't think about passing it by reference.
Yep Chervil's is definitely the way to go. Using references for fstreams is definitely the better way, to prevent any weirdness in compiler-version differences, and just for clarity if nothing else (so you know you're only looking at the file with one stream).

And just to clarify my previous post: If you compile with C++11/14/17, my code will work, but if you compile with C++98 (and change filename to filename.c_str() as needed), you will get errors related to the constructor of the ios_base/basic_ifstream when returning masterfile. I suppose it has to do with move semantics? C++11 also deletes the copy constructor.
Topic archived. No new replies allowed.