【发布时间】:2016-03-04 08:05:13
【问题描述】:
我刚开始学习文件,我知道如何设置它并让它工作。我必须编写这个程序,我必须允许用户输入一些信息并让用户也使用二进制更新和调整任何数据。 所以我可以一直写到用户可以写入和读取文件的地步。但我不知道如何让用户调整数据或添加数据。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class client {
public:
string name;
int balance;
string id;
};
int main()
{
int ans;
int x;
string nameIn;
string adjName;
client client1;
ofstream out("client1.dat", ios::binary);
cout << "\nDo you want to add information or update info" << endl;
cin >> ans;
if (ans == 1)
{
cout << "\nPlease enter the name of your client" << endl;
cin >> nameIn;
x = nameIn.length();
if (x <= 10)
{
for (int i; i < 10; i++)
{
adjName[i] = nameIn[i];
}
}
else
{
for (int i = x; i < 10; i++)
{
adjName[i] = ' ';
}
}
client1.name = adjName;
cout << "\nPlease enter the balance of your client" << endl;
cin >> client1.balance;
cout << "\nPlease enter the id of your client" << endl;
cin >> client1.id;
cout << "\nThe name of your client is " << endl << client1.name
<< endl << "\nThe balance of your client is " << endl
<< client1.balance << endl << "\nThe id of your client is "
<< endl << client1.id;
out.write(reinterpret_cast<const char*> (&client1), sizeof(client));
}
/*
else if (ans == 2)
{
string answer, newName,line;
cout << "\nWhat name do you want to update? " << endl;
cin >> answer;
cout << "\nWhat is the new name?" << endl;
cin >> newName;
if (out)
}
*/
system("pause");
return 0;
}
所以名称只需 10 个字符长,以便我们可以调整/更新它。它编译并运行,但每次编译器到达它检查名称长度的部分时,它都会吓坏并说“调试断言失败” 字符串下标超出范围。
还有关于这段代码的问题——如果我在没有将名称调整为特定数组长度的位的情况下运行它,程序就会运行,并很好地存储所有内容。但是当我尝试读回 .dat 时,它会读回它,但会因访问冲突而退出,迫使我手动停止调试。我做错了什么?
这是读取文件的代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class client {
public:
string name;
int balance;
string id;
};
int main()
{
client client1;
char ans;
cout << "\nDo you want to view the information about your client?"
<< endl;
cin >> ans;
ifstream in("client1.dat", ios::binary);
if (ans == 'y' || ans == 'Y')
{
in.read(reinterpret_cast<char*> (&client1), sizeof(client));
cout << "The name is " << endl << client1.name << endl
<< "The balance is " << endl << client1.balance << endl
<< "The id is " << endl << client1.id << endl;
}
system("pause");
return 0;
}
【问题讨论】:
-
您不能将非 POD 类型(如
std::string)序列化为二进制文件。std::string内部持有指向堆上数据的指针,无法通过从文件中读取数据来恢复。 -
您好,请您澄清一下。荚?我仍然是初学者,有点迷失文件。这和 reinterpret_cast 有关系吗?
-
欢迎来到 Stack Overflow。对于您要解决的问题,此代码太长且太复杂。在编写代码时,您应该单独开发新功能,在寻求帮助时,您应该发布minimal complete example。您似乎在询问几个错误;选择一个并准备尝试做一件事的代码,如果它不起作用,请向我们展示。
-
Plain Old D数据类型。
-
“这和reinterpret_cast有关系吗”是的。
标签: c++ file append binaryfiles