Reputation: 37
I am relatively new to C++ and I wanted to practice file opening and putting text in, now I realize this would be the worst way to store login info, but it is just how I chose to simulate it as at least it wont be totally random. Now I am doing very well in all but one place it seems as I keep getting errors the whole code is
#include <iostream>
#include <string>
#include <fstream>
#include <new>
using namespace std;
string login() {
string username, password;
cout << "What is your username?\n";
cin >> username;
cout << "What is your password, " << username << endl;
cin >> password;
//Verify info
return username;
}
string signup() {
string username, password, cpass, bio;
do {
cout << "What is your username?\n";
cin >> username;
cout << "What is your password?\n";
cin >> password;
cout << "Confirm password: ";
cin >> cpass;
cout << "Describe what you like to do:\n";
cin >> bio;
} while (password != cpass);
ofstream user = new ofstream();
user("users.txt");
if (user.is_open()) {
//Make sure the program is writing to the end of the file!
user.seekp(0,std::ios::end);
user << username << endl;
user << password << endl;
user << bio << endl;
} else {
cout << "Something went wrong with opening the file!";
}
user.close();
return username;
}
int main() {
string answ;
cout << "Hello, welcome to wewillscamyou.net, are you already signed up?\n";
if(answ == "Yes" || answ == "yes") {
string username = login();
} else {
string username = signup();
}
return 0;
}
but I am getting errors at these two lines, it is not because of a typo, and I need help because this would work in java:
ofstream user = new ofstream();
user("users.txt");
Upvotes: 1
Views: 143
Reputation:
Buddy in C++ new
is used to create dynamically allocated object, or object that you have pointer to, or that you have to allocate memory for. Usually that is pointer to an object.
class A {
public:
A() { }
};
int main () {
A a (); // object (created as value)
A *a = new A(); // notice pointer, I need to allocate memory for it thus I have to use `new`
}
In conclusion new
in C++ means allocate enough memory for this object and give me the address of it. So to solve your error you have several choices:
ofstream user ("user.txt");
or
ofstream user;
user = ofstream("users.txt");
or
ofstream user;
user.open("user.txt");
...
user.close("user.txt");
user("users.txt");
Upvotes: 2
Reputation: 29
'ofstream' is used to write in a text or binary file. While 'new' is used to allocate memory. To write in the end of a file you need to first open it in 'append'(app) mode.It will automatically use the memory in your storage drive once its get connected to a file.
**user.seekp(0,std::ios::end);**
This line of code is not wrong but not required.
Replace this
ofstream user = new ofstream();
user("users.txt");
if (user.is_open()) {
//Make sure the program is writing to the end of the file!
user.seekp(0,std::ios::end);
user << username << endl;
user << password << endl;
user << bio << endl;
}
by this:-
ofstream user("user.txt",ios::app);
if(user)
{
user << username << endl;
user << password << endl;
user << bio << endl;
}
Upvotes: 1
Reputation: 6125
Pass the filename to the ofstream
constructor. Also, specify that you want to append to the file - there is no need to seek manually.
ofstream user("users.txt", ofstream::app);
if (user)
{
user << username << endl;
user << password << endl;
user << bio << endl;
}
else
{
cout << "Something went wrong with opening the file!";
}
Upvotes: 0