Passing Filestream functions ifstream or ofstream

Hello, there. I am trying to open a txt file. There are many text files that I will be opening in the code that is why I am trying to create a function where you can print the string inside the txt file but I am having an error. What can I do to solve this problem? Thank 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
bool txtOpen(string s, ofstream& f);
void txtClose(ofstream& f);
void txtPrint(ofstream& f);

	ofstream processor; // I dont know if it should be ofstream or ifstream to pass in the argument.
	
	isOpen = txtOpen("processor.txt", processor);
	
	if(isOpen)
	{
		txtPrint(processor);
		
		txtClose(processor);
	}
	else
	{
		cout <<"Error. Stop right now!! \n";	
	}

bool txtOpen(string s, ofstream& f) 
{
	f.open(s);
	if(f.is_open())
		return true;  
	else 
		return false;
}
void txtClose(ofstream& f)
{
	f.close();
}
void txtPrint(ofstream& f)
{
	string line;
	getline(f.c_str,line);
	while(f);
	{
		cout << line << endl;
		getline(f,line);
	}
}
Last edited on
Your lines 5 through 18 aren't part of a function, so that appears to be the most glaring issue.

Second issue is that you have a semi-colon on line 36, making your loop an infinite loop:
1
2
3
4
while (condition);
{
    statements;
}

means
1
2
3
4
5
6
7
8
while (condition)
{
    // nothing
}

{
    statements;
}


ofstream = OUTPUT stream. You write to an output stream.
ifstream = INPUT stream. You read from an input stream.
If you're trying to open an existing file, you want an input stream.
Hope that helps.
Last edited on
Use an input file stream. (std::ofstream is for output).

File streams are moveable; we can use move semantics if we want. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>

std::ifstream open_text( const std::string& file_name )
{
    return std::ifstream(file_name) ;
}

bool print_file( std::ifstream text_file )
{
    return bool( std::cout << text_file.rdbuf() ) ;
}

int main()
{
    print_file( open_text(__FILE__ ) ) ;
}


http://coliru.stacked-crooked.com/a/59a38a52e072236e
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
#include <fstream>
#include <string>
#include <iostream>

bool txtOpen(const std::string& s, std::ifstream& f);
void txtClose(std::ifstream& f);
void txtRead(std::ifstream& f);

int main()
{
	std::ifstream processor;

	if (txtOpen("processor.txt", processor)) {
		txtRead(processor);
		txtClose(processor);
	} else
		std::cout << "Error. Stop right now!! \n";
}

bool txtOpen(const std::string& s, std::ifstream& f)
{
	f.open(s);

	return f.is_open();
}

void txtClose(std::ifstream& f)
{
	f.close();
}

void txtRead(std::ifstream& f)
{
	for (std::string line; getline(f, line); std::cout << line << '\n');
}

Last edited on
Thank you for your help guys. Now, I can move on in this problem!
If you pass ifstream by ref in a function, then you can do things like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>

std::ifstream open_text(const std::string& file_name)
{
	return std::ifstream(file_name);
}

bool print_file(std::ifstream& text_file)
{
	return bool(std::cout << text_file.rdbuf());
}

int main()
{
	auto f {open_text(__FILE__)};

	print_file(f);
}


which if ifstream is passed by value in print_file() you can't - as it won't compile.
Topic archived. No new replies allowed.