0

我正在使用二进制文件中的 ifstream 读取列表以构建数组。这是我的构建数组函数:

    int buildArrays (Player players[])
{

ifstream inFile;

inFile.open( "binary_hockey", ios::binary );

if ( inFile.fail() )
   {
   cout << "The binary_hockey input file did not open";
   return 0;
   }


Player onePlayer;
int index = 1;

inFile.read( (char *) &onePlayer, sizeof(Player) );

while (inFile)
{
    index++;
    inFile.read( (char *) &onePlayer, sizeof(Player) );
}

inFile.close();
return index-1;

}

然后我使用这个函数在屏幕上打印数组。

void printArrays( Player players[], int numPlayers)
{

    int i = 0;
    int pnts;

while((i)<numPlayers)     //keeps displaying the info as long as num players is greater the array building up t
        {

    pnts = players[i].goals + players[i].assists;

    cout<<left<<setw(20)<<players[i].name<<setw(10)<<players[i].goals<<setw(10)<<players[i].assists<<setw(10)<<pnts << endl;

    i++;
        }

    cout<<endl<<endl<<endl;
}

结果是我从二进制列表中获得了 unicode 字符和非常大的数字,而不是名称和单个数字。我在这里做错了什么?

4

1 回答 1

1

你没有读入players. 您的代码应如下所示:

while (inFile)
{
 inFile.read( (char *) &players[index++], sizeof(Player) );
}
于 2013-04-10T23:40:31.430 回答