gpt4 book ai didi

c++ - 所以我无法理解 C++ 中的文件

转载 作者:行者123 更新时间:2023-11-30 05:35:01 25 4
gpt4 key购买 nike

我刚开始学习文件,我知道如何设置它并让它工作。我必须编写这个程序,我必须允许用户输入一些信息并让用户使用二进制更新和调整任何数据。所以我可以一直写到用户可以写入和读取文件的程度。但是我不知道如何让用户调整数据或添加数据。

#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;
}

最佳答案

至于第一部分:

 for (int i; i < 10; i++)
// ^

未初始化 i归零。如果输入小于 10 个字符怎么办?您将访问 std::string出界。您应该更换 if/else并简单地循环

 adjName = nameIn;
while(adjName.length() <= 10) {
adjName += ' ';
}

摆脱调试断言。


对于问题的第二部分,正如评论中已经提到的,您不能使用包含 std::string 类的结构来执行此操作.

reinterpret_cast<char*> (&client1)只是混淆了 std::string在内部使用指向动态分配的字符数据的指针,并且在稍后读取存储的数据时无法有意义地恢复(因此您会遇到访问冲突)。

一个可行的方法可能是使用类似的东西

struct client {
char name[11];
int balance;
char id[5];
};

我猜你需要为家庭作业做这个,为此目的可能就足够了。

但是您很快就会看到缺点,即字符数据的大小需要固定,并且您不能有任意长度的字符串。我永远不会将这样的代码用于生产就绪代码。

另一个陷阱(同样提到过)是,int对于不同的 CPU 体系结构,以相同的方式表示(使用的字节顺序,即字节顺序)。所以二进制文件不能在不同的计算机上移植使用。


最简单的解决方案是不使用二进制文件,而是使用文本格式的文件并重载 std::ostream& operator<<(std::ostream&, const client&)std::istream& operator>>(std::istream&, client&)输出/输入运算符。

或者使用一些第 3 方库,如 boost::serialization或 google protocol buffers,支持二进制文件的反序列​​化/序列化。

关于c++ - 所以我无法理解 C++ 中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34002805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com