2016-12-24 45 views
0

我无法读取30行的内容.csv文件后,进入下面的结构一个结构:无法读取.csv文件到一个类,它表示一个矢量

class Entry { 
public: 
    std::string GID; 
    std::string DName; 
    std::string Phone; 
    std::string POC; 
    std::string Item; 
    std::string Category; 
    double amount; 
}; 

我希望能够创建一个可以读取名为“Donations.csv”的csv文件的函数,并删除逗号,因为我想创建一个可能类似于如下的向量:std::vector<Entry> entries;,这样我就可以根据向量中的任何元素对多个项目进行排序。因此,例如说我想通过DNAME排序,代码会是这个样子:

// sort by DName 
std::sort(entries.begin(), entries.end(), [](const Entry& a, const Entry& b) { 
    return a.DName.compare(b.DName) < 0; 
}); 

我被告知不要使用多个载体,因为这并不安全,但有效的,如果它可以帮助别人,这是如何读取一个CSV文件分成多个向量:
公平的警告,采用EOF控制器是错误的,REFER HERE
虽然,这个代码将工作刚刚好,我亲自测试了 - 上的.csv无数次文件。

int main() 
{ 
ifstream inFile;     // Input file handler 
ofstream outFile;     // Output file handler 

vector<string> GID;     // Vector holding Gift ID 
vector<string> DName;    // Vector holding Donor Name 
vector<string> Phone;    // Vector holding Phone Number 
vector<string> POC;     // Vector holding point of contact 
vector<string> Item;    // Vector holding item donated 
vector<string> Category;   // Vector holding type of item donated 
vector<double> amount;    // Vector holding amount of donated items 

int Input;       // User selection choice from menu 

// opening data file 
inFile.open("Donations.csv"); 
if (!inFile)      // If statement to check for file erroe 
{ 
    cout << "Input file did not open. Program will exit." << endl;  // Does not compute! 
    exit(0); 
} 

// reading contents 
string str;       // temp string to hold parse comma 
double temp;      // temp double to hold null spaces for atoi function/strtod 

getline(inFile, str, ',');   // read first GID string outside of loop so that we 
            // won't accidentally reach the end of the file in the MIDDLE of the loop 

while (!inFile.eof())    // end of file not reached 
{ 
    GID.push_back(str); 
    getline(inFile, str, ',');  // get the contents of the line until a comma is encountered and push it into the necessary vector 
    DName.push_back(str); 
    getline(inFile, str, ','); 
    Phone.push_back(str); 
    getline(inFile, str, ','); 
    POC.push_back(str); 
    getline(inFile, str, ','); 
    Item.push_back(str); 
    getline(inFile, str, ','); 
    Category.push_back(str); 
    getline(inFile, str, '\n');  // get until the end of the line but split for parsing 

    // convert string to double using strtod and c_str 
    temp = strtod(str.c_str(), NULL); 
    // now push double onto vector 
    amount.push_back(temp); 
    getline(inFile, str, ',');  // read in GID here instead of the top. If we are at the end 
            // of the file, it won't work and end-of-file flag will be set which is what we want. 
} 
inFile.close(); 
} 
+1

“我有麻烦”是不是一个有用的问题说明。从代码的偶然检查中,有几个显而易见的问题 - 来自'while(!inFile.eof())'是一个明显的bug(请参阅http://stackoverflow.com/questions/5431941/why-is- while-feof-file-always-wrong)到使用'getline()'的脆弱,容易出错的解析 - 但没有一个特定的问题,没有具体的答案是可能的。 –

+0

@SamVarshavchik谢谢先生,我已经重述了这个问题。谢谢'while(!inFile.eof())'的提醒。我会更新这篇文章以反映这一点。 –

+0

@SamVarshavchik我只是想知道如何读取一个csv文件到一个结构,并从输入文件中删除逗号 –

回答

-1

此方法添加:

friend std::istream& operator>>(std::istream& input, Entry& e); 

Entry结构。您的容器

std::istream& 
operator>>(std::istream& input, Entry& e) 
{ 
    input >> e.GID; 
    input >> e.Dname; 
    //.. input other members 
    return input; 
} 

更改为:

添加到您的源代码

std::vector<Entry> database; 

你输入回路会是这个样子:

Entry e; 
while (my_data_file >> e) 
{ 
    database.push_back(e); 
} 

您可能还需要在互联网上搜索“stackoverflow C++读取文件结构”以获取更多示例。

总之,创建一个结构或类来建模记录。
重载操作符>>读取记录。
使用结构的std::vector,而不是每个数据成员的向量。
如果你认真对待一个数据库,那么使用一个;不要重写你自己的。

编辑1:排序
为了使排序更加容易,过载>操作:

struct Entry 
{ 
    //... 
    bool operator<(const Entry& other) const 
    { 
    // add comparison functionality, example: 
    return GID < other.GID; 
    } 
} 

现在,您可以通过使用数据库排序:

std::sort(database.begin(), database.end()); 

编辑2:其他分拣
要订购容器使用不同的标准,你可以c reate排序函数,并将它们传递给std::sort功能:

bool Order_By_Dname(const Entry& e1, const Entry& e2) 
{ 
    return e1.Dname < e2.Dname; 
} 

您的排序变为:

std::sort(database.begin(), database.end(), Order_By_Dname); 
+0

谢谢先生,我可能不了解所有这些,但是,我很努力地实施这些代码。再次谢谢你! –

+0

有人在乎解释downvote? –

+0

'database.push_back(e);'没有解析csv文件的逗号。这就是我想的,我已经照顾好了。由于我是新的,我的upvote不算。尽管感谢您的帮助。有用! –