我在创建读取文件的函数时遇到问题

时间:2013-11-21 04:53:04

标签: c++ arrays

我真的不知道如何去做。它以逗号分隔。 我的所有其他功能都有效,但我似乎无法创建一个读取功能。 有人可以帮忙吗? 该文件看起来像这样..

170, Abelia,  Glossy  Abelia x grandiflora,  Caprifoliaceae
222, Almond,  Dwarf Russian  Prunus tenella,  Rosaceae
85, Almond,  Halls Hardy  Prunus dulcis,  Rosaceae
200, Apple,  Early Harvest  Malus x domestica,  Rosaceae
246, Apple,  Lodi  Malus x domestica,  Rosaceae
121, Apple,  Red Delicious  Malus x domestica,  Rosaceae
225, Apple,  Red Jonathan  Malus x domestica,  Rosaceae
114, Apple,  Stayman Winesap  Malus x domestica,  Rosaceae
148, Apple,  Yellow Delicious  Malus x domestica,  Rosaceae
77, Apricot,  Early Golden  Prunus armeniaca,  Rosaceae
138, Apricot,  Moorpark  Prunus armeniaca,  Rosaceae
84, Arborvitae Nigra American,  Thuja occidentalis  Nigra,  Cupressaceae
79, Arborvitae Woodward Globe,  Thuja occidentalis  Woodwardii,  Cupressaceae
247, Arborvitae,  American  Thuja occidentalis,  Cupressaceae
93, Arborvitae,  Emerald  Thuja occidentalis,  Emerald, Cupressaceae
144, Arborvitae,  Golden Globe  Thuja occidentalis,  Golden Globe, Cupressaceae
42, Arborvitae,  Green Giant  Thuja standishii x plicata Green Giant,  Cupressaceae
77, Ash,  Green  Fraxinus pennsylvanica,  Oleaceae
208, Ash,  White  Fraxinus americana,  Oleaceae
98, Aspen,  Quaking  Populus tremuloides,  Salicaceae
140, Azalea Formosa,  Azalea indica  Formosa,  Ericaceae
127, Azaleas Mollis Hybrid,  Orange  Rhododendron x kosteranum,  Ericaceae
181, Azaleas Mollis Hybrid,  Pink  Rhododendron x kosteranum,  Ericaceae
146, Azaleas Mollis Hybrid,  Red  Rhododendron x kosteranum,  Ericaceae
248, Azaleas Mollis Hybrid,  White  Rhododendron x kosteranum,  Ericaceae
127, Azaleas Mollis Hybrid,  Yellow  Rhododendron x kosteranum,  Ericaceae
158, Baldcypress,  Taxodium  distichum,  Taxodiaceae
41, Beautybush,  Kolkwitzia  amabilis,  Caprifoliaceae
164, Beech,  American  Fagus grandifolia,  Fagaceae
78, Beech,  European  Fagus sylvatica,  Fagaceae
231, Birch,  Paper  Betula papyrifera,  Betulaceae
6, Birch,  River  Betula nigra,  Betulaceae
118, Birch,  Yellow  Betula alleghaniensis,  Betulaceae
200, Black Tupelo (Black Gum),  Nyssa  sylvatica,  Cornaceae
215, Boxwood (Common),  Buxus  sempervirens,  Buxaceae
119, Boxwood (Korean),  Buxus sinica var  insularis,  Buxaceae
144, Boxwood Green Velvet,  Buxus sinica var  insularis x B. sempervirens Green Velvet,  Buxaceae
30, Buckeye,  Red  Aesculus pavia,  Hippocastanaceae
15, Buckeye,  Yellow  Aesculus flava (octandra),  Hippocastanaceae
9, Butternut (White Walnut),  Juglans  cinerea,  Juglandaceae
90, Camellia,  Camellia  sasanqua,  Theaceae

1 个答案:

答案 0 :(得分:2)

在您阅读数据后不确定要对数据执行什么操作,但这会将每行放入向量中。另请注意,为此需要更改argc ...在Xcode中执行此操作,转到产品菜单,然后进行方案,编辑方案,单击+并键入文件路径

#include <iostream>
#include <iomanip>  // for setw() and ws
#include <string>
#include <fstream>
#include <cstdlib>
#include<vector>

using namespace std;
/* function prototype(s) */
void addContents(ifstream&);

int main(int argc, char* argv[])
{
    if (argc < 2) {
        cerr << "Usage: " << argv[0] << "/path/to/your/file" << endl;
        exit (1);
    }
    ifstream datafile {argv[1]}; /* first arg is filename */
    if (!datafile.is_open()) {
        cout << "Can't read input from " << argv[1] << endl;
        exit (1);
    }

    addContents(datafile);
    datafile.close();
    return 0;
}

void addContents(ifstream& ff){
    vector<string> fileContents;
    string currentLine;
    while(getline(ff, currentLine)){
        fileContents.push_back(currentLine);
    }
    for (int i = 0; i<fileContents.size(); i++){
        cout << fileContents.at(i) << endl;
    }
}