2013-11-21 86 views
-2

我真的不知道該如何去做。它由逗號分隔。 我所有的其他功能都可以工作,但是我似乎無法創建讀取功能。 任何人都可以幫忙嗎? 文件看起來是這樣的..我在創建讀取文件的函數時遇到了問題

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 

回答

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; 
    } 
}