为什么从二进制文本读取时会得到unicodeand大数字?

时间:2013-04-10 23:31:33

标签: c++ c list binary fstream

我正在使用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字符和非常大的数字,而不是二进制列表中的名字和单个数字。我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

您不是在阅读players。您的代码应如下所示:

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