1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
struct News
{
std::string m_category;
std::string m_Event_Id;
std::string m_priority;
std::string m_abstract;
News(){}
News (const std::string& category, const std::string& Event_Id,
const std::string& priority, const std::string& abstract)
: m_category(category), m_Event_Id(Event_Id), m_priority(priority), m_abstract(abstract){}
};
std::ostream& operator << (std::ostream& os, const News& n)
{
os << n.m_category << ", " << n.m_Event_Id << ", " << n.m_priority << ", " << n.m_abstract << "\n";
return os;
}
int main()
{
std::ifstream inFile {"D:\\test.txt"};
std::vector<News> vecNews{};
if(inFile)
{
std::string line{};
while (getline(inFile, line))
{
std::istringstream stream{line};
std::string category{}, Event_Id{}, priority{}, abstract{};
getline(stream, category, ',')
&& getline(stream, Event_Id, ',')
&& getline (stream, priority, ',')
&& getline(stream, abstract);
if(inFile)
{
vecNews.emplace_back(News(category, Event_Id, priority, abstract));
}
}
}
for (const auto& elem : vecNews)std::cout << elem ;
}
|