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

问题描述 投票:1回答:1

我目前正在为我的项目制定一个会计程序。我正在努力从文件读取到嵌套结构。关于我应该去哪里的任何指导?我知道我希望它停止读入帐户数据,并在我到达空白行(空终止符)时转移到下一个客户。基本上,一些客户有几个账户,有些客户有两个以上(但不超过5个,结构数组只有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
c++ struct fstream c-strings
1个回答
0
投票

我注意到的问题:

Problem 1

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

我假设你打算使用strncpy,而不是strncpys。即使这样,10也太小了。你需要使用20。

Problem 2

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

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

Problem 3

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

Problem 4

检查内部while循环中空行的逻辑是有缺陷的。你用过了

    cin.getline(line, 20);

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

    fin.getline(line, 20);

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


但是,我提出的更重要的建议是:

  1. 重新考虑解析输入文件的策略。我发现最简单的方法是逐行读取文件的内容,然后分别处理每一行。
  2. 创建较小的函数来读取输入的不同部分并从main中使用它们。
  3. 写下stdout你读的内容,这样你就可以清楚地看到数据未被读取的地方,就像你希望的那样。

我建议对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);
}
© www.soinside.com 2019 - 2024. All rights reserved.