写入二进制文件不起作用
Write to binary file not working
下面的代码编译成功 运行 但退出时没有任何内容写入文件。文件已创建,在选择注册时它会询问所有详细信息并显示消息注册 successful.THE 文件 customers.data
是空的(文件大小为 0kb)。函数write
没有写对象到file.All的东西似乎是正确的code.Can没查出bug。请帮忙
主程序:
int main()
{
int ch;
Customer cust;
fstream file;
file.open("customers.data",ios::out|ios::app|ios::binary);
if(!file)
{
cout<<"\nError Files are missing. Unable to create files\n";
system("pause");
exit(1);
}
cout << "WELCOME! Press any key to continue.\n\n";
system("pause");
do
{
system("cls");
cout<<"\n\n\n";
cout<<"1->Login"<<endl;
cout<<"2->Register"<<endl;
cout<<"3->Exit"<<endl;
cout<<"Choose An option: ";
cin>>ch;
switch(ch)
{
case 1:
if(cust.Login()==true)
{
LaunchCustomerMenu();
}
else
{
cout<<"Incorrect Email/Password\n";
system("pause");
}
break;
case 2:
cust.Register();
file.write((char*)&cust, sizeof(cust)); //does not work
if(!file.fail()) //still true
{
cout<<"\n\nRegistration Successful"<<endl;
system("pause");
}
else
{
cout<<"\nTheir was a Error processing your Registration\n";
system("pause");
}
break;
case 3:
exit(0);
break;
default:
cout<<"\n\nWRONG OPTION\n";
cout<<"Choose Again\n\n";
system("pause");
}
cin.ignore();
}
while(ch!=3);
file.close();
return 0;
}
class:
class Customer
{
private:
unsigned int CustomerID;
string CustomerName;
string CustomerAddress;
string CustomerEmail;
string CustomerPassword;
unsigned int CustomerPhone;
public:
Customer();
void Register();
bool Login();
unsigned int getID();
string getName();
string getEmail();
string readPassword();
unsigned int getPhone();
unsigned int RandomID();
void modifyName();
void modifyAddress();
void modifyEmail();
void modifyPassword();
void modifyPhone();
};
CustomerID
随机生成
函数寄存器:
void Customer::Register()
{
cout<<"Enter Your Name: ";
cin.ignore();
getline(cin,CustomerName);
cout<<"Enter Your Address: ";
cin.ignore();
getline(cin,CustomerAddress);
cout<<"Enter Your Email: ";
cin>>CustomerEmail;
cout<<"Enter A Password: ";
cin.ignore();
getline(cin,CustomerPassword);
cout<<"Enter Your Phone no. :";
cin>>CustomerPhone;
}
编辑:
根据下面的答案,我添加了对 file.fail()
的检查,但它 returns false 并且 customers.data
为空,即 write
操作无法写入对象。
fstream write
调用 return 对自身的引用,因此选项 3 的 if 检查总是 return 为真。您需要调用 good
、bad
或 fail
函数之一来检查它是否真的成功了。
此外,将对象的实际内容写入任何地方(套接字、磁盘等)通常不是一个好主意。您应该查找序列化的概念。这是将 class 数据写入磁盘的正确方法。
我也遇到了同样的情况,我设法找到了错误。
当使用传统的 char
变量类型来声明一个数组来保存字符串值时,它工作正常。
它们似乎与字符串库的实现有关。即使您能够以某种方式将值写入具有字符串数据类型的文件,在读取值时也会生成 运行 time error(SIGSEGV) 。我认为该字符串使用指针进行动态变量分配,不适合用于文件。生成 SIGSEGV 作为分配的变量 space 在 运行 程序后不再保留在内存中。
下面的代码编译成功 运行 但退出时没有任何内容写入文件。文件已创建,在选择注册时它会询问所有详细信息并显示消息注册 successful.THE 文件 customers.data
是空的(文件大小为 0kb)。函数write
没有写对象到file.All的东西似乎是正确的code.Can没查出bug。请帮忙
主程序:
int main()
{
int ch;
Customer cust;
fstream file;
file.open("customers.data",ios::out|ios::app|ios::binary);
if(!file)
{
cout<<"\nError Files are missing. Unable to create files\n";
system("pause");
exit(1);
}
cout << "WELCOME! Press any key to continue.\n\n";
system("pause");
do
{
system("cls");
cout<<"\n\n\n";
cout<<"1->Login"<<endl;
cout<<"2->Register"<<endl;
cout<<"3->Exit"<<endl;
cout<<"Choose An option: ";
cin>>ch;
switch(ch)
{
case 1:
if(cust.Login()==true)
{
LaunchCustomerMenu();
}
else
{
cout<<"Incorrect Email/Password\n";
system("pause");
}
break;
case 2:
cust.Register();
file.write((char*)&cust, sizeof(cust)); //does not work
if(!file.fail()) //still true
{
cout<<"\n\nRegistration Successful"<<endl;
system("pause");
}
else
{
cout<<"\nTheir was a Error processing your Registration\n";
system("pause");
}
break;
case 3:
exit(0);
break;
default:
cout<<"\n\nWRONG OPTION\n";
cout<<"Choose Again\n\n";
system("pause");
}
cin.ignore();
}
while(ch!=3);
file.close();
return 0;
}
class:
class Customer
{
private:
unsigned int CustomerID;
string CustomerName;
string CustomerAddress;
string CustomerEmail;
string CustomerPassword;
unsigned int CustomerPhone;
public:
Customer();
void Register();
bool Login();
unsigned int getID();
string getName();
string getEmail();
string readPassword();
unsigned int getPhone();
unsigned int RandomID();
void modifyName();
void modifyAddress();
void modifyEmail();
void modifyPassword();
void modifyPhone();
};
CustomerID
随机生成
函数寄存器:
void Customer::Register()
{
cout<<"Enter Your Name: ";
cin.ignore();
getline(cin,CustomerName);
cout<<"Enter Your Address: ";
cin.ignore();
getline(cin,CustomerAddress);
cout<<"Enter Your Email: ";
cin>>CustomerEmail;
cout<<"Enter A Password: ";
cin.ignore();
getline(cin,CustomerPassword);
cout<<"Enter Your Phone no. :";
cin>>CustomerPhone;
}
编辑:
根据下面的答案,我添加了对 file.fail()
的检查,但它 returns false 并且 customers.data
为空,即 write
操作无法写入对象。
fstream write
调用 return 对自身的引用,因此选项 3 的 if 检查总是 return 为真。您需要调用 good
、bad
或 fail
函数之一来检查它是否真的成功了。
此外,将对象的实际内容写入任何地方(套接字、磁盘等)通常不是一个好主意。您应该查找序列化的概念。这是将 class 数据写入磁盘的正确方法。
我也遇到了同样的情况,我设法找到了错误。
当使用传统的 char
变量类型来声明一个数组来保存字符串值时,它工作正常。
它们似乎与字符串库的实现有关。即使您能够以某种方式将值写入具有字符串数据类型的文件,在读取值时也会生成 运行 time error(SIGSEGV) 。我认为该字符串使用指针进行动态变量分配,不适合用于文件。生成 SIGSEGV 作为分配的变量 space 在 运行 程序后不再保留在内存中。