所以我在理解C++文件中时遇到问题

So I'm having trouble understanding files in C++

本文关键字:文件 遇到 问题 C++      更新时间:2023-10-16

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

#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或谷歌协议缓冲区,支持对二进制文件的反序列化。