当我关闭程序并在c++中再次运行时,如何阻止我的记录被覆盖
How can I stop my records from overwriting when i close the program and run it again in c++
我试图将一些用户输入保存到dat文件中,当程序关闭并再次打开时,我想将新记录保存到我已经得到的记录上。。我不确定最新的命令是什么来读取数据并在屏幕上显示我读过书,但它们都有旧的命令,但不起作用。
//account.h
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <String>
#define _CRT_SECURE_NO_DEPRECATE
#include <fstream>
#include <stdlib.h>
using namespace std;
struct Account
{
public :
void login();
void userRegister();
protected :
int username;
int password;
};
void Account :: userRegister()
{
Account myAccount;
char terminator;
int myUserID = 0;
int myPassword = 0;
std::cout << "Enter Your 8 digit UserID";
cin >>myUserID ;
cout << "Enter Your 6 digit password";
cin >> myPassword ;
FILE * dat = fopen("Accounts.dat", "wb");
myAccount.userID ;
myAccount.password ;
fwrite(&myAccount, sizeof(myAccount), 1, dat);
//cin.get (terminator);
fclose (dat);
}
以追加模式打开文件。
这里a
告诉附加,如果文件不存在,则+
创建文件。
FILE * dat = fopen("Accounts.dat", "ab+");
http://www.cplusplus.com/reference/cstdio/fopen/
您似乎在询问如何将新数据添加到现有文件中。要做到这一点,您需要在附加模式下打开文件。
更改
FILE * dat = fopen("Accounts.dat", "wb");
至
FILE * dat = fopen("Accounts.dat", "ba+");
相关文章:
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- 即使在我关闭应用程序并重新启动它后,如何保持我的 ID 被阻止?
- 阻止我的项目调用__CxxFrameHandler3(CRT函数)
- 我怎样才能阻止我的程序输出时间,它使我的程序难以阅读
- 我的C++程序被Windows Defender阻止和删除
- 为什么我可以在不链接任何额外库的情况下包含 sys/*.h
- 我收到阻止我运行程序的警告,但不确定如何解决
- 错误代码LNK1561阻止我的代码运行
- 如何阻止编译器删除我的功能
- 如何阻止我的数组打印地址
- 如何防止我的屏幕截图制作控制台程序阻止屏幕截图
- 我如何才能阻止我的精灵在SFML中在空中颠簸
- 气泡排序阻止我的程序执行
- 如何阻止我的函数返回 0
- c ++ 如何阻止我的循环两次吐出最后一个输入
- 为什么这个基本cin会阻止我的程序编译
- 正在阻止我的应用程序的多个实例
- 为什么在此方法中添加cout行可以阻止我的程序在Windows中触发缓冲区溢出异常?
- 我怎么能阻止我的整数显示为十六进制
- 当我关闭程序并在c++中再次运行时,如何阻止我的记录被覆盖