嵌套结构,从一个空行分隔的文件中读取数据

Nested Structures, reading data from a file separated by a blank line

我目前正在为我的项目开发会计程序。我正在努力从文件中读入嵌套结构。关于我应该去哪里的任何指导?我知道我希望它停止读取帐户数据并在我到达空白行(空终止符)时转到下一位客户。基本上,一些客户有几个帐户,其他客户有超过 2 个(但不超过 5 个,struct 数组只包含 5 个)。项目本身更深入(结构包含更多变量)。我目前只是在使用练习文件来尝试弄清楚这个概念..

到目前为止,这是我的代码:

 struct ACCOUNT
 {
 char acct_num[7];
 char balance[8];
 };

 struct CUSTOMER
{ 
char cust_name[20];
char number[5];
ACCOUNT acct[5];
};

int main()
{
  CUSTOMER person[3];
  fstream fin;
  fin.open("accounts.dat", ios::in);
  int i, j;
  i = 0, j = 0;
  char buff[20];
  char line[20];
  while (fin.getline(buff, 20))
    {
    strncpys(person[i].cust_name, buff, 10);
    fin.getline(line, 10);
    strncpy(person[i].number, line, 10);


    do {
        fin.getline(line, 20, ' ');
        strncpy(person[i].acct[j].acct_num, line, 10);
        fin.getline(line, 20);
        strncpy(person[i].acct[j].balance, line, 10);
        j++;
        cin.getline(line, 20);

    } while (*line != 0);
    i++;
}
return 0;
}

我正在尝试读入的数据文件:

Jane Smith
FD12
SSDFSS 64.51
SD5545 88.51

John Smith
FD45
SFG789 77.21
NM4521 21.223
MM7888 33.33

John Doe
FSS4
SFGSGG 77.65
HN5555 22.31

我注意到的问题:

问题 1

strncpys(person[i].cust_name, buff, 10);

我假设您打算使用 strncpy,而不是 strncpys。即便如此,10 还是太小了。你需要使用 20.

问题 2

strncpy(person[i].number, line, 10);

在这里,10 太大了。您需要使用 5.

问题 3

j需要在外循环内重新初始化为0

问题 4

内部 while 循环中检查空行的逻辑存在缺陷。您已使用

    cin.getline(line, 20);

作为循环中的最后一行,但我认为这是发布到 SO 时的错误。我想你有

    fin.getline(line, 20);

这是一个问题,因为您使用了文件的下一行,但其中包含的数据只是被丢弃了。


不过,我比较重要的建议是:

  1. 重新考虑解析输入文件的策略。我发现逐行读取文件内容然后分别处理每一行是最简单的。

  2. 创建较小的函数来读取输入的不同部分并使用它们 main

  3. 将您读取的内容写入标准输出,以便您可以清楚地看到数据未按您希望的方式读取。

我建议对 main 进行以下更新。

int main()
{
   CUSTOMER person[3];
   fstream fin;
   fin.open("socc.in", ios::in);
   int i = 0;

   std::string line;
   while ( getline(fin, line) )
   {
      read_customer_name(line, person[i]);
      std::cout << "cust_name: " << person[i].cust_name << std::endl;

      if ( getline(fin, line) )
      {
         read_customer_number(line, person[i]);
         std::cout << "number: " << person[i].number << std::endl;
      }
      else
      {
         // Read was not successful.
         // Break the loop.
         break;
      }

      // Define it in this scope only.
      int j = 0;
      while ( getline(fin, line) )
      {
         if ( line.empty() )
         {
            break;
         }

         read_customer_account(line, person[i].acct[j]);
         std::cout << "acct[j].acct_num: " << person[i].acct[j].acct_num << std::endl;
         std::cout << "acct[j].balance: " << person[i].acct[j].balance << std::endl;

         j++;
      }
   }
   return 0;
}

辅助函数在哪里:

void read_customer_name(std::string const& line, CUSTOMER& person)
{
   strncpy(person.cust_name, line.c_str(), 20);
}

void read_customer_number(std::string const& line, CUSTOMER& person)
{
   strncpy(person.number, line.c_str(), 5);
}

void read_customer_account(std::string const& line, ACCOUNT& acct)
{
   std::istringstream str(line);
   str.getline(acct.acct_num, 10, ' ');
   str.getline(acct.balance, 10);
}