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

时间:2015-11-30 16:04:21

标签: c++ file append binaryfiles

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

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

1 个答案:

答案 0 :(得分:1)

第1部分:

 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&)输出/输入运算符。

或者使用某些第三方库,例如boost::serialization或Google协议缓冲区,它们支持对二进制文件进行解除/序列化。