【问题标题】:So I'm having trouble understanding files in C++所以我无法理解 C++ 中的文件
【发布时间】: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


【解决方案1】:

至于第一部分:

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

未能将i 初始化为零。另外,如果输入小于 10 个字符怎么办?您将越界访问std::string。您应该替换 if/else 和简单的循环

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

摆脱调试断言。


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

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

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

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

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

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

另一个陷阱(如前所述)是,int 在不同 CPU 架构中的表示方式不同(使用的字节顺序,即字节序)。所以二进制文件不能移植到不同的计算机上。


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

或者使用一些 3rd 方库,例如 boost::serialization 或 google 协议缓冲区,它们支持对二进制文件进行反序列化/序列化。

【讨论】:

  • 好的,我按照您的指示进行操作,这就是我要去的地方
  • @JackFaber 很好,如果您认为该答案有帮助并解决了您的问题,请考虑通过单击左侧的大白色复选标记来接受它:-) ...
  • 我无法在此处发布代码,因此我将其发布为答案 - 请您查看一下。谢谢
  • @JackFaber 这不是答案,但可能会跟进问题。请发布另一个问题。而且你实际上并没有按照我的指示。
  • @JackFaber 你错过了std::string 字符不能在没有std::string 以适当长度初始化的情况下被任意访问。这就是为什么我建议完全按照我的示例所示编写 while 循环。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-22
  • 1970-01-01
  • 2019-06-27
  • 1970-01-01
  • 2011-10-23
  • 1970-01-01
  • 2021-01-22
相关资源
最近更新 更多