繁体   English   中英

用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)模式打开它。一旦文件连接到文件,它将自动使用存储驱动器中的内存。

**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 ++ new Buddy用于创建动态分配的对象,具有指针或必须为其分配内存的对象。 通常这是指向对象的指针。

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`
}

总而言之,C ++中的new意味着为该对象分配足够的内存并给我它的地址。 因此,要解决您的错误,您有几种选择:

ofstream user ("user.txt");

要么

ofstream user;
user = ofstream("users.txt");

要么

ofstream user;
user.open("user.txt");
...
user.close("user.txt");
user("users.txt");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM