Open file based on user input

I'm trying to open a file for read operations and open another file for write operations, but already in the beginning there are problems compiling it.
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main () {
	string filename;
	char inputfilename, outputfilename;

	ifstream inputfile;
	while(!inputfile.is_open()) {
		cout << "Input filename: ";
		getline (cin, inputfilename);
		inputfile.open(inputfilename,ios::in | ios::binary);
	}

	ofstream outputfile;
	while(!inputfile.is_open()) {
		cout << "Output filename: ";
		getline (cin, outputfilename);
		outputfile.open(outputfilename,ios::out | ios::binary);
	}
	
	system("pause");
	return 0;
}

But it can't be compiled.

Full error code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1
1>Compiling...
1>livc++.cpp
1>.\livc++.cpp(13) : error C2664: 'void std::basic_ifstream<_Elem,_Traits>::open(const wchar_t *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::string' to 'const wchar_t *'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>.\livc++.cpp(20) : error C2664: 'void std::basic_ofstream<_Elem,_Traits>::open(const wchar_t *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::string' to 'const wchar_t *'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>Build log was saved at "file://c:\Users\Toby Hinloopen\Documents\Visual Studio 2005\Projects\livc++\livc++\Release\BuildLog.htm"
1>livc++ - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Based on the error, i think i need to convert the string to "const wchar_t *", but i've never heard of that variable type.

Another thing,
system("pause") calls "pause.exe"... doesn't it?
is there another way (exept for "while(1){}") to stop the application from closing after the function is comlete?


FIXED using
1
2
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
and "#include <limits>"

FIXED even better with:
1
2
3
4
5
#include <limits>
void pause() {
	std::cout << "Press ENTER to continue...";
	std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
}

in the top of the code, right under al the other includes
Last edited on
Try making inputfilename and outputfilename strings on line 8. Then I think you'll need to also call the strings' c_str() methods to pass them to the open functions.
Huh????
open() doesn't take a const wchar_t *, it takes a const char *!
WTF?
Just as a tip if you type:
using namespace std;

before your main(), you can avoid typing the std::'s everywhere.
FIXED using:
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

#include <limits>
void pause() {
	cout << "Press ENTER to continue...";
	cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
}

int main () {
	string filename,inputfilename, outputfilename;
	ifstream inputfile;
	while(!inputfile.is_open()) {
		cout << "Input filename: ";
		getline (cin, inputfilename);
		inputfile.open(inputfilename.c_str(),ios::in | ios::binary);
	}

	ofstream outputfile;
	while(!outputfile.is_open()) {
		cout << "Output filename: ";
		getline (cin, outputfilename);
		outputfile.open(outputfilename.c_str(),ios::out | ios::binary);
	}
	
	pause();
	return 0;
}

thanks to seymore.
Topic archived. No new replies allowed.