You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++文件I/O如何保存数据 解决重启程序后文件内容被清空问题

问题根因

你程序启动时直接以默认模式打开userDB.txt作为输出流,C++中ofstream默认打开参数是ios::out,该模式会直接截断文件(也就是清空所有原有内容),不管你后续是否执行写入操作,所以每次重启应用之前存储的用户数据都会丢失。

修复步骤
  • 不要在程序启动阶段就打开userDB.txt的写流,仅在需要写入新注册用户数据时再打开,同时指定ios::app追加模式,新内容会追加到文件末尾,不会覆盖原有数据,文件不存在时也会自动创建。
  • 补充变量初始化逻辑:你的number变量声明后未赋值就直接做条件判断,属于未定义行为,需要先给用户展示功能菜单让用户输入选择后再走对应分支。
  • 普通用户登录分支需要补充读取userDB.txt已有数据做账号密码校验的逻辑,原代码这部分是空的。
修改后的参考代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
double amount;
int main () {
    ifstream fin;
    string username,password, adminUsername, adminPassword,newusername,newpassword; 
    // 仅读取管理员账号,这里提前打开没问题
    fin.open("admin.txt");
    if(!fin.is_open()) {
        cout << "管理员配置文件admin.txt不存在,请先创建该文件并写入管理员账号密码" << endl;
        return 1;
    }
    fin >> adminUsername;
    fin >> adminPassword;
    fin.close();

    // 先初始化number,展示主菜单
    int number;
    cout << "请选择功能:\n1.普通用户登录\n2.管理员入口\n请输入选项:";
    cin >> number;

    if(number == 1){
        cout << "Welcome to the Normal User Login Page : \n\n";
        cout << "Enter Username : ";
        cin >> username;
        cout << "Enter password : ";
        cin >> password;
        // 读取userDB.txt校验账号密码
        ifstream userFin("userDB.txt");
        bool loginSuccess = false;
        string u, p;
        double a;
        while(userFin >> u >> p >> a) {
            if(u == username && p == password) {
                loginSuccess = true;
                cout << "登录成功,你的余额是:" << a << endl;
                break;
            }
        }
        userFin.close();
        if(!loginSuccess) {
            cout << "账号或密码错误" << endl;
        }
    }
    else if(number == 2) {
        cout << "Welcome to the Registration and Deposit Page\n";
        cout << "Enter administrator username and password.\n";
        cout << "Enter username : ";
        cin >> username;
        cout << endl;
        cout << "Enter Password : ";
        cin >> password;
        if(username == adminUsername){
            if(password == adminPassword){
                cout << "Welcome admin \n";
                cout << "1. Deposit money for client\n";
                cout << "2. Register new client\n";
                cout << "3. Reset your password \n\n";
                cout << "Enter option to proceed : ";
                cin >> number;
                if(number==2){
                    cout << "Client register form\n";
                    cout << "Enter client username : ";
                    cin >> newusername;
                    cout << "Enter client password : ";
                    cin >> newpassword;
                    cout << "initial deposit : ";
                    cin >> amount;
                    // 仅在需要写入的时候打开,用追加模式
                    ofstream fout("userDB.txt", ios::app);
                    fout << newusername << endl;
                    fout << newpassword << endl;
                    fout << amount << endl;
                    fout.close();
                    cout << "用户注册成功" << endl;
                }
            }
            else{
                cout << "Wrong password";
            }   
        }
        else{
            cout << "Wrong UserName";
        }
    }
    return 0;
}

内容的提问来源于stack exchange,提问作者Serge Kambale

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 06:06:03