can not create file

So i have problem with files. Idea is to open files with different names using one function, and write some data to them, also want them to have file extensions:

void openFile (filename, somedata) {
ofstream file;
file.open(filename + ".txt"); // .txt for example, any other is OK
file << somedata;
file.close();
}

The result should be that we have many files created automatically like "John.marks, Clara.marks" or "usa.cities, russia.cities".

Is there something i am doing wrong, 'cause it worked before.

Also here is the error message i am getting

[Error] no matching function for call to 'std::basic_ofstream<char>::open(std::basic_string<char>, const openmode&)'
Show your WHOLE code, including headers. I take it that you do actually have
#include <fstream>
?
lastchance, sorry, yeah, here it is

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// login register system
/*
	vars: u_pswd, u_name, sys_pswd, sys_name, u_input
	func: login(), reg(), menu()
	How:
	- Menu: login or register
		- Register: enter u_name && u_paswd
					save to file
					goto main menu
		- login: 	(*)enter u_name, check if valid
				 	e.g. if there is file with u_name name
				 	(*)read sys_pswd from file
				 	input u_pswd, compare
				 	*if either u_name or u_pswd not valid:
				 		- error msg
				 		- goto main menu
*/

#include <iostream>
#include <fstream>
//#include <>

using namespace std;

bool login ();
void reg ();
void menu ();

int main () {
	string u_name, u_pswd;
	string sys_name, sys_pswd;
	
	menu();
}

void menu () {
	string u_input;
	
	cout << "Welcome!\n";
	cout << "Login|Register\n";
	cin >> u_input;
	
	if (u_input == "login" || u_input == "Login") {
		login();
	}
	else if (u_input == "register" || u_input == "Register") {
		reg();
	}
	else {
		cerr << "\nInvalid input, try again!\n\n";
		system("pause");
		system("cls");
		menu();
	}
}

bool login () {
	
}

void reg () {
	system("cls");
	string u_name, u_pswd;
	string sys_name, sys_pswd;
	
	cout << "Register\n";
	cout << "Enter username:	";
	cin >> u_name;
	cout << "Enter password:	";
	cin >> u_pswd;
	
	sys_name = u_name;
	sys_pswd = u_pswd;
	
	ofstream fileout;
	fileout.open(sys_name + ".login");
	fileout << sys_pswd;
	fileout.close();
}
It compiles perfectly OK on cpp.sh (click the "edit and run" button next to your code sample). (Minor warning that bool login() doesn't return anything yet, but that's not your issue.)

If I run it on my PC, and type "register" (you could do with a better prompt) then I can enter username and password and it creates the appropriate file. (I won't comment on whether single files for each user is actually a good idea or not.)

So your error message doesn't correspond to your posted code. You've got your versions muddled up, perhaps?
Last edited on
Possibly you aren't compiling using the latest c++ version? Originally .open() didn't support a std::string type as a file name and you had to use a c-str type. This was fixed in version xxx (I forget which). As the error message refers to .open(), this could be the issue.
lastchance, seeplus,

Thank you very much! it seems that i misclicked and mousewheeled a menu in IDE which changed the compiler settings.

You rescued me from a long depression)
Topic archived. No new replies allowed.