Passing file names as formal parameters from main to function in another .cpp file

I am trying to pass file names as formal parameters to a function in a separate .cpp file where the files will be opened and processed. I am able to open the files from within main, but would like to break that out into the separate function. I am pretty new to C++, so thanks in advance for your patience.

I am getting compile errors on this line of main():
 
openFilesForEncrypt(ifstream plainTextFile, ofstream cipherFile);

The compile errors are:
Expected primary-expression before 'plainTextFile'
Expected primary-expression before 'cipherFile'

Here is the code snippet from main():

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
#include "a06headerDylanVance.h"
int main() 
{
//variables
	char choice;
	int shift;
	string plainTextFile;
	string cipherFile;

// a bunch of user input not relevant (I think)
switch (choice) 
		{
			case 'e':
				cout << "Please tell me the name of the file to encrypt: ";
				cin >> plainTextFile;
				cout << endl << "Please tell me the name of the plaintext file" 
				" to write: ";
				cin >> cipherFile;
				
				openFilesForEncrypt(ifstream plainTextFile, ofstream cipherFile);
				
				encryptCaesar(shift); // stub
				break;
			case 'd':
				decryptCaesar(shift); // stub
				break;
			default:
				cout << "Something is amiss.  Exiting program." << endl;
				break;
		}
 


Here's how the Prototype looks in the .h file:
 
bool openFilesForEncrypt(ifstream & plainTextFile, ofstream & cipherFile);

This compiles without error.

The called function in the 2nd .cpp file also has errors on compile with these two lines:
1
2
	inFile.open(plainTextFile.c_str());
	outFile.open(cipherFile.c_str());

The error is:
'struct std::basic_ifstream<char, std::char_traits<char> >' has no member named 'c_str'

Here's how the function being called in the 2nd .cpp file looks:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "a06headerDylanVance.h"

bool openFilesForEncrypt(ifstream & plainTextFile, ofstream & cipherFile)
{
	//bool openPlainTextFile = true;
	//bool openCipherFile = true;
	ifstream inFile;
	ofstream outFile;
	
	inFile.open(plainTextFile.c_str());
	outFile.open(cipherFile.c_str());
	
//this function is nowhere near complete... just trying to open the files at this point.
	
	return 0;
	
	
}

void encryptCaesar(int shift)
{
	cout << "encryptCaesar(shift) function" << endl; // stub
	cout << shift;
}


Thanks again for taking the time to look at this.
-dtv
Last edited on
Why are you passing std::ifstream and std::ofstream objects to the openFilesForEncryption() function?
You probably meant to pass the strings plainTextFile and cipherFile which you declared in main(), because that's how you're trying to treat those objects. What confuses me is that even though you treat the file streams as strings, you still pre-pended each function argument with a file stream type, as if to re-declare them????
Thanks for the reply. You're right it works when I stop trying to pass std::ifstream and std::ofstream objects, and instead pass the file names as strings.

Clearly, I have much to learn!

Thanks,
-dtv
Topic archived. No new replies allowed.