C ++通过字符串指针从.txt文件导入数据

时间:2014-07-09 20:13:15

标签: c++

players.txt

2 
Zlatan 
Ibrahimovic 
1981 
4 
20130110 
20130117 
20130122 
20130208 
Yohan 
Cabaye 
1986 
1 
20130301 

Main.cpp的

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
    ifstream pFile("players.txt");

    int numberOfPlayers;
    string firstName;
    string lastName;
    int birthYear;

    int numberOfMatches = 0;

    string *matchDates = NULL;
    matchDates = new string[];

    while (pFile >> numberOfPlayers >> firstName >> lastName >> birthYear >> numberOfMatches >> matchDates)
    {
        for(int i = 0; i < numberOfPlayers; i++)
        {   
            cout << firstName << endl;
            cout << lastName << endl;
            cout << birthYear << endl;
            for(int i = 0; i < numberOfMatches; i++)
            {
                cout << matchDates[i] << endl;
            }
        }
    }

    delete[] matchDates;

    getchar();
    return 0;
}

所以我想从文本文件导入数据,例如如上所述的足球运动员或其他任何东西,如曲棍球运动员等。重点是能够将此代码用于所有类型的运动,也就是说它不会设置为最多12名运动员,运动员的数量应该是动态的。好吧,我已经被我认为是动态数组(matchDates)所困扰,但我已经被告知它不是,并且仍然是一个静态数组。

匹配日期也应该是动态的,因为一些玩家可能有20个数学,另一个可能有五个,另外三个等等。在我的例子中,zlatan有四个和你的一个。

我的问题出现在第20行,发布在下面:

while(pFile&gt;&gt; numberOfPlayers&gt;&gt; firstName&gt;&gt; lastName&gt;&gt; birthYear&gt;&gt; numberOfMatches &gt;&gt; matchDates)

将鼠标悬停在粗体&#34;&gt;&gt;&#34;上时出现错误消息(在VS中用红色加下划线)

错误无操作员&#34;&gt;&gt;&#34;匹配这些操作数 操作数类型是:std :: basic_istream&gt; &GT;&GT; std :: string *

我想打印出所有匹配日期,但它不起作用,我非常确定它与matchDates有关。

提前致谢并问及亚当

3 个答案:

答案 0 :(得分:0)

你可以把东西放在一行来简化代码,但不要过度。将“记录”作为一条记录,你可以说。

数组没有&gt;&gt;操作

会给用户0x499602D2他的答案,但我可能还没有投票或评论,所以我把它放在这里,因为我已经回答了。 ;)

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
    ifstream pFile( "players.txt" );

    int numberOfPlayers;
    string firstName;
    string lastName;
    int birthYear;

    int numberOfMatches = 0;

    while( pFile >> numberOfPlayers )
    {
        for( int i = 0; i < numberOfPlayers; i++ )
        {


            pFile >> firstName >> lastName >> birthYear >> numberOfMatches;

            cout << firstName << endl;
            cout << lastName << endl;
            cout << birthYear << endl;

            for( int i = 0; i < numberOfMatches; i++ )
            {
                string date;
                pFile >> date;
                cout << date << endl;
            }
        }
    }

    getchar();
    return 0;
}

<小时/> 要维护数据,您可以创建结构(或类)。第二个while循环只是在第一个获取数据时打印出来。

    #include <iostream>
    #include <string>
    #include <fstream>
    #include <vector>
    using namespace std;

    struct Player
    {
        string firstName;
        string lastName;
        int birthYear;
        std::vector<string> matchDates;
    };

    int main()
    {
        ifstream pFile( "players.txt" );

        int numberOfPlayers;
        int numberOfMatches = 0;

        std::vector<Player> players;

        // fill the vector players
        while( pFile >> numberOfPlayers )
        {

            for( int i = 0; i < numberOfPlayers; i++ )
            {

                // create player struct and fill it
                Player player;

                pFile >> player.firstName >> player.lastName >> player.birthYear >> numberOfMatches;

                for( int i = 0; i < numberOfMatches; i++ )
                {
                    string date;
                    pFile >> date;
                    player.matchDates.push_back( date );
                }

                // add it to the vector
                players.push_back( player );

            }

        }

        // print out the values
        std::vector<Player>::iterator iterPlayer = players.begin(),
                                                                    endPlayer = players.end;
        while( iterPlayer != endPlayer )
        {

            Player player = *iterPlayer;

            cout << player.firstName << endl;
            cout << player.lastName << endl;
            cout << player.birthYear << endl;

            std::vector<string>::iterator iterDates = player.matchDates.begin(),
                                                                        endDates = player.matchDates.end;
            while( iterDates != endDates )
            {
                string date = *iterDates;
                cout << date << endl;
            }
        }

        getchar();
        return 0;

    }

答案 1 :(得分:0)

对播放器使用C ++类,停止使用new和override stream operator。

查看行动中的代码here

#include <iostream>
#include <string>
#include <deque>
#include <fstream>
#include <algorithm>
#include <iterator>
using namespace std;

class Player
{
public:
    Player() : firstName(""), lastName(""), birthYear(0){}
    ~Player() {}
    //copy ctor by default

    // Operator Overloaders
    friend ostream &operator <<(ostream &output, const Player p);
    friend istream &operator >>(istream &input, Player &p);

private:
    string firstName;
    string lastName;
    int birthYear;
    deque<string> matchDates;
};

ostream& operator <<( ostream& output, const Player p )
{
    output << p.firstName << " " << p.lastName << " etc ...";
    return output;
}

istream& operator >>( istream& input, Player &p )
{
    input >> p.firstName;
    input >> p.lastName;
    input >> p.birthYear;
    int nbMatch = 0; input >> nbMatch;
    for(int i = 0 ; i < nbMatch ; ++i) {
        string match_date; input >>  match_date;
        p.matchDates.push_back(match_date);
    }

    return input;
}//*/

int main()
{
    //ifstream pFile("players.txt");

    int numberOfPlayers;
    deque<Player> players;

    cin >> numberOfPlayers; int i = 0;
    while( i <  numberOfPlayers)
    {
        Player p;
        cin >> p;
        players.push_back(p);

        i++;
    }

    std::ostream_iterator< Player > output( cout, "\n" );
    cout << "What we pushed into our deque: ";
    copy( players.begin(), players.end(), output );

    getchar();
    return 0;
}

答案 2 :(得分:0)

问题

这是Clang给我的错误:

error: invalid operands to binary expression ('istream' and 'string *')

然后继续列出operator>>()的各种重载以及std::string*类型的右手操作数无法转换为相应的右手类型的原因。这是预期的,因为没有operator>>()的重载采用该类型的指针或数组。

解决方案

读入数组时,通常会读入“临时”对象并将其复制到数组中。例如:

#include <iostream>

int main()
{
    int array[5];

    for (int i = 0, temp; i < 5 && std::cin >> temp; ++i)
    {
        array[i] = temp;
    }
}

但是在你的情况下,你使用以下行初始化你的数组,我认为这是尝试创建一个无限大小的数组:

 matchDates = new string[];

我不知道Visual Studio,但是在Clang上我收到了以下错误:

Error(s):

error: expected expression
    int* j = new int[];
                     ^
1 error generated

C ++中无法使用动态大小的数组。如果您需要一个大小会增加的数组,请考虑使用std::vector。您需要调用push_back()来插入元素。