C ++ - 尝试创建一个函数,将文件中的数据读入结构数组?

时间:2014-05-14 19:16:23

标签: c++ arrays function visual-c++ struct

我看了几个类似的问题,但我似乎还无法理解这一点。我得到了结构和数组的基本思想,但我真的迷失了实际创建一个函数来将文件中的数据读入结构数组。希望有人可以帮助我。 :(

我们必须:

  
      
  1. 编写一个函数InputPlayerData,它将未知>长度的输入文件中的数据读入结构数组
  2.         

    一个。输入文件中的数据包含有关每个播放器的以下信息

         
        
          
    • 名称

    •     
    • 职位

    •     
    • 号码(球员号码在她的球衣上)

    •     
    • 击球时(球员击球的次数)

    •     
    • 命中(击球时的命中数)

    •     
    • 采取的基础(击中后的基数)
    •     
      
         

    B中。输入文件中的数据行:

         

    我。第一位玩家的数据:

         
        
          
    • MarTee Apple 是玩家的名字

    •     
    • 捕手是玩家的位置

    •     
    • 17 是她的球衣号码

    •     
    • 20 是击球的次数

    •     
    • 7 是点击次数

    •     
    • 20 是所采取的基数

    •     
      

所以,这就是它的样子。我试图启动该功能,程序实际运行,但我没有得到输出。光标只是闪烁。任何帮助都会非常感激。

struct playerRecordType
{ 
    string name;
    string position;
    int number;
    int atBats;
    int hits;
    int basesTaken;
    int ranking;
    double battingAverage;
    double sluggingAverage;
};


int InputPlayerData(ifstream& inFile, playerRecordType player[MAX]);

int main(void)
{
    ifstream inFile;
    ofstream outFile;

    //open the files
    inFile.open("SoftballData.txt");
    outFile.open("SoftballResults.txt");

    playerRecordType player[MAX];

    int length = InputPlayerData(inFile, player);

    outFile << length;

    return 0;
}


int InputPlayerData(ifstream& inFile, playerRecordType player[])
{
     int index;

    //initialize i
    index = 0;

    //primer for the loop
    getline(inFile, player[index].name);

   //while not end-of-file to process the file data
   while(!inFile.eof())
   {
            inFile >> player[index].name >> player[index].position 
                    >> player[index].number >> player[index].atBats
                >> player[index].hits >> player[index].basesTaken;

        index++;        
   }

2 个答案:

答案 0 :(得分:1)

首先,这个:

while(!inFile.eof()) 

错了。 Read this article为什么会这样。

接下来,您的循环体和初始入口不正确。你这样做:

//primer for the loop
getline(inFile, player[index].name);

显然尝试加载第一个玩家的名字,然后在循环体中继续执行此操作:

inFile >> player[index].name >> player[index].position
       >> player[index].number >> player[index].atBats
       >> player[index].hits >> player[index].basesTaken;

请注意,播放器的名称行是两次。这将抛弃此记录的剩余数据,最后一个元素basesTaken尝试提取(并失败)从第二个播放器名称中提取整数。结果是流将处于错误状态,但尚未达到EOF。因此,添加侮辱伤害前面讨论的不正确的while循环条件永远不会成立,循环将再次执行,并且由于错误状态,所有提取失败,再次循环,还没有eof等...

我猜想这就是你要做的事情:

#include <iostream>
#include <fstream>
#include <string>

struct playerRecordType
{
    std::string name;
    std::string position;
    int number;
    int atBats;
    int hits;
    int basesTaken;
    int ranking;
    double battingAverage;
    double sluggingAverage;
};

inline std::istream& operator >>(std::istream& inp, playerRecordType& player)
{
    if (std::getline(inp, player.name) &&
        std::getline(inp, player.position) &&
        (inp >> player.number >> player.atBats >> player.hits >> player.basesTaken))
    {
        if (player.atBats > 0)
        {
            player.battingAverage = static_cast<double>(player.hits)/
                                    static_cast<double>(player.atBats);
            player.sluggingAverage = static_cast<double>(player.basesTaken)/
                                     static_cast<double>(player.atBats);
        }
        else
        {
            player.battingAverage = 0;
            player.sluggingAverage = 0;
        }
    };

    return inp;
}


template<std::size_t N>
std::size_t InputPlayerData(std::istream& inp, playerRecordType (&players)[N])
{
    std::size_t x = 0;
    for (; x < N && inp >> players[x]; ++x);
    return x;
}

#define MAX (100)

int main()
{
    std::ifstream inFile("SoftballData.txt");
    playerRecordType players[MAX];

    std::size_t length = InputPlayerData(inFile, players);
    std::cout << length << '\n';

    return 0;
}

我把这个小任务的输出端留给深蓝色代码作为练习。祝你好运。

答案 1 :(得分:-1)

就像0x499602D2用户评论一样 - 您的InputPlayerData()方法没有返回任何值。