C++ 文件打开
file opening in C++
我是 C++ 的新手,我想练习打开文件和输入文本,现在我意识到这将是存储登录信息的最糟糕的方式,但这正是我选择模拟它的方式至少它不会完全随机。现在我在所有方面都做得很好,除了一个地方似乎我一直在出错,整个代码是
#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;
}
但我在这两行遇到错误,这不是因为打字错误,我需要帮助,因为这在 java:
中有效
ofstream user = new ofstream();
user("users.txt");
将文件名传递给 ofstream
构造函数。此外,指定您要附加到文件 - 无需手动查找。
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!";
}
'ofstream'用于写入文本或二进制文件。而 'new' 用于分配内存。
要写入文件末尾,您需要先在 'append'(app) 中打开它 mode.It 一旦连接到文件,将自动使用存储驱动器中的内存。
**user.seekp(0,std::ios::end);**
这行代码没有错,但不是必须的
替换这个
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;
}
通过这个:-
ofstream user("user.txt",ios::app);
if(user)
{
user << username << endl;
user << password << endl;
user << bio << endl;
}
C++ 中的 Buddy new
用于创建 动态分配的 对象,或者您有指针指向的对象,或者您必须为其分配内存的对象。通常是指向对象的指针。
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`
}
结论new
在C++中意味着为这个对象分配足够的内存并给我它的地址。因此,要解决您的错误,您有多种选择:
ofstream user ("user.txt");
或
ofstream user;
user = ofstream("users.txt");
或
ofstream user;
user.open("user.txt");
...
user.close("user.txt");
user("users.txt");
我是 C++ 的新手,我想练习打开文件和输入文本,现在我意识到这将是存储登录信息的最糟糕的方式,但这正是我选择模拟它的方式至少它不会完全随机。现在我在所有方面都做得很好,除了一个地方似乎我一直在出错,整个代码是
#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;
}
但我在这两行遇到错误,这不是因为打字错误,我需要帮助,因为这在 java:
中有效ofstream user = new ofstream();
user("users.txt");
将文件名传递给 ofstream
构造函数。此外,指定您要附加到文件 - 无需手动查找。
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!";
}
'ofstream'用于写入文本或二进制文件。而 'new' 用于分配内存。 要写入文件末尾,您需要先在 'append'(app) 中打开它 mode.It 一旦连接到文件,将自动使用存储驱动器中的内存。
**user.seekp(0,std::ios::end);**
这行代码没有错,但不是必须的
替换这个
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;
}
通过这个:-
ofstream user("user.txt",ios::app);
if(user)
{
user << username << endl;
user << password << endl;
user << bio << endl;
}
C++ 中的 Buddy new
用于创建 动态分配的 对象,或者您有指针指向的对象,或者您必须为其分配内存的对象。通常是指向对象的指针。
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`
}
结论new
在C++中意味着为这个对象分配足够的内存并给我它的地址。因此,要解决您的错误,您有多种选择:
ofstream user ("user.txt");
或
ofstream user;
user = ofstream("users.txt");
或
ofstream user;
user.open("user.txt");
...
user.close("user.txt");
user("users.txt");