fstream as function arguments

Hi (again) :)

I am rather new in C++ (or coding in general for that purpose) and am working on a function that causes me a lot of trouble...

My function is supposed to take 4 parameters: 1 ifstream 1 ofstream and 2 strings (filenames for ifstream and ofstream). Then it is supposed to:
- open the ifstream and ofstream files using the filenames from the string arguments. Before that the strings need to be converted to cstrings using strVar.c_str().
- Read the content of the ifstream file (1 word/string) and write it to the ofstream.
- Close the ifstream and ofstream
- Report the success

Both text files are in the same folder like the .cpp. The "read" file (user needs to input it) is "text.txt" and contains 1 word "example" to be read.
The outputfile "text_new.txt" is empty.

Here's my attempt that gets around 1x10^6 errors... so I am in desperate need of advise. I am sure this could be done easier (no function necessary), but for educational purposes I'd like to know how to do this. Thanks so much in advance!

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

using namespace std;

void fileManipulation(istream& infile, ostream& outfile, string& infilename, string& outfilename);


int main()
{
	// opening a file with user input filename
	string read;
	string write = "text_new.txt";
	
	cout << "Please enter the filename to open..." << endl;
	cin >> read;
	
	ifstream infile_stream;
	ofstream outfile_stream;
	
	fileManipulation(infile_stream, outfile_stream,  read, write);
	
	return 0;
}

void fileManipulation(istream& infile, ostream& outfile, string& infilename, string& outfilename)
{
	string data;
	
         // open fstreams, read from ifstream and write to ofstream
	infile.open(infilename.c_str());
	infile >> data;
	
	outfile.open(outfilename.c_str());
	outfile << data;
	
	infile.close();
	outfile.close();	
}
closed account (Dy7SLyTq)
i dont believe istream and ostream have member open. i would just make them ifstream and ofstream instead
Yep, that was it, thanks DTSCode!
Topic archived. No new replies allowed.