Cannot write to file.

For some reason this piece of code won't write the info to the txt file. I've been looking for a while and can't seem to find anything. Does anybody have any ideas? Thanks 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
	else if (!hasAnAccount) //If they need to register
	{
		//promptRegistration(); //Prompt Registration
		string newUsername;
		string newPassword;

		userFile.open("usernameList.txt", ios::out || ios::trunc || ios::in || ios::app);
		passFile.open("passList.txt", ios::out || ios::trunc || ios::in || ios::app);

		if (!userFile.is_open() || !passFile.is_open()) {
			cerr << "Error opening password files...\n";
		}

		cout << "What would you like your username to be?\t";
		cin >> newUsername;
		cout << "\nWhat would you like your password to be?\t";
		cin >> newPassword;

		userFile << newUsername << "\n";  //Appends the new username to the list
		passFile << newPassword << "\n";  //Appends the new password to the list

		for (int i = 0; i < numberOfAccounts; ++i) {
			usernames.resize(usernames.size() + 1);
			passwords.resize(passwords.size() + 1);

			getline(userFile, usernames[i]);
			getline(passFile, passwords[i]);
			cout << "Loading...\n";
		}

		userFile.close();
		passFile.close();

		++numberOfAccounts;

		cout << "Thank you for registering, you may now login.\n";
what does it do?
the most common issue is path...

try putting the full folder path to the file in the open statement, or make sure the executable, and where the executable is called from if run from your IDE, are able to get to that file. Operating systems are pretty picky about this.
Thanks for the quick response. Essentially it is just supposed to append the new username and password to the txt file. I'm using visual c++ and I was able to access the file and use the information using ifstream just fine, so I didn't think that the path was an issue.
What is the type of stream for userFile and passFile? And where are they defined?

1
2
		userFile.open("usernameList.txt", ios::out || ios::trunc || ios::in || ios::app); // This will fail!
		passFile.open("passList.txt", ios::out || ios::trunc || ios::in || ios::app); // This will fail! 

Be careful with all of those open modes.

From: http://www.cplusplus.com/reference/fstream/fstream/fstream/

If the mode has both trunc and app set, the opening operation fails. It also fails if trunc is set but out is not.


Topic archived. No new replies allowed.