所以我在理解C++文件中时遇到问题
So I'm having trouble understanding files in C++
我刚开始学习文件,我知道如何设置它并使其工作。我必须编写这个程序,允许用户输入一些信息,并让用户使用二进制更新和调整任何数据。因此,我可以一直写到用户可以对文件进行写入和读取的地步。但我不知道如何让用户调整数据或添加数据。
#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];
};
我想你需要这样做作为家庭作业练习,出于这个目的,这可能就足够了。
但您很快就会发现缺点,即字符数据的大小需要固定,并且不能有任意长度的字符串。我从来不会把这样的代码用于生产。
另一个陷阱(如前所述)是,对于不同的CPU架构,int
没有以相同的方式(使用的字节顺序,即端序)表示。因此,二进制文件不能在不同的计算机上便携使用。
最简单的解决方案不是使用二进制文件,而是使用文本格式的文件,并重载std::ostream& operator<<(std::ostream&, const client&)
和std::istream& operator>>(std::istream&, client&)
输出/输入运算符。
或者使用一些第三方库,如boost::serialization
或谷歌协议缓冲区,支持对二进制文件的反序列化。
- 如何从文本文件中读取数值,直到遇到字符类型?
- 创建 Spdlog 异步文件记录器时遇到困难
- 我在使用C++文件时遇到了一些问题
- 我在 ndk 中构建文件时遇到错误 android.mk
- 我遇到使用 c++ 逐行读取输入文件的问题
- 从文件导入数据时遇到问题.我有一个没完没了的循环
- 在visual Studio中构建代码时,我遇到错误,.h文件丢失
- 在C++中打开 json 文件时遇到问题
- C++读取文件时遇到困难
- 我在从.txt文件中加起来时遇到了一些问题
- C++击球平均值程序使用输入/输出文件打印名称,平均值,最高/最低平均值时遇到麻烦
- 在 64 位 Linux armv8 计算机上编译 32 位二进制文件时遇到问题
- 在C++中使用 Visual Studio 读取文本文件时遇到问题
- 使用 libzip 打开 zip 文件时遇到问题
- 我在编写从文本文件中读取数据并按升序打印的代码时遇到问题
- 当所有内容都在一个文件中时,为什么会遇到链接器错误
- 当文件遇到特定字符时,如何停止读取文件
- 在此任务上遇到麻烦,我们需要使用指针和数组来找到文本文件中某些数字的平均值,中值和模式
- 我在让我的代码输出整个链表时遇到问题.它只是输出我修改过的一些文本文件,而不是整个东西
- C++处理文件,在制作程序时遇到麻烦