@Gokilla

Записать данные в структуры c++ аналитика?

Есть файл с базой данных orders_class.txt
orderItemID;orderDate;deliveryDate;itemID;size;color;manufacturerID;price;customerID;salutation;dateOfBirth;state;creationDate
1;2013-04-01;2013-04-03;2347;43;magenta;1;89.90;12489;Mrs;1963-04-26;Hesse;2012-04-23
2;2013-04-01;2013-04-03;2741;43;grey;1;99.90;12489;Mrs;1963-04-26;Hesse;2012-04-23
3;2013-04-01;2013-04-03;2514;9;ecru;19;79.90;12489;Mrs;1963-04-26;Hesse;2012-04-23
4;2013-04-01;2013-05-06;2347;42;brown;1;89.90;12489;Mrs;1963-04-26;Hesse;2012-04-23
5;2013-04-01;?;2690;43;grey;1;119.90;12489;Mrs;1963-04-26;Hesse;2012-04-23
6;2013-04-01;2013-04-02;2318;41;blue;1;89.90;3649;Mrs;1956-03-26;Hesse;2013-04-01
7;2013-04-01;2013-04-22;2590;7+;white;57;99.90;3649;Mrs;1956-03-26;Hesse;2013-04-01
8;2013-04-01;2013-06-13;2656;41;magenta;1;79.90;3649;Mrs;1956-03-26;Hesse;2013-04-01
9;2013-04-01;2013-04-02;335;34;purple;51;29.90;22284;Mrs;1967-02-08;Rhineland-Palatinate;2011-02-16
...

и есть вот такие структуры

Есть вот такой код
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
struct Customer
{
	int customerID;
	std::string salutation;
	std::string dateOfBirth;
	std::string state;
	std::string creationDate;
};
struct Item
{
	int itemID;
	std::string size;
	std::string color;
	int manufacturerID;
	double price;
};
struct Order
{
	int orderItemID;
	std::string orderDate;
	std::string deliveryDate;
	Customer *customer;
	Item *item;
};

int main()
{
	std::cout << "Hello world!" << std::endl;
	std::string str;
	std::ifstream file("C:\\Users\\User\\Desktop\\инт сис\\04\\orders_class.txt", std::ios::in);
	std::vector<Order>*vec = new std::vector<Order>();
	while (!file.eof())
	{
		getline(file, str);
		//std::cout << str << std::endl;
	};
	file.close();
	system("pause");
	return 0;
}

каким образом мне считать данные в вектор для дальнейшей обработки?
  • Вопрос задан
  • 51 просмотр
Решения вопроса 1
@Gokilla Автор вопроса
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int pos;
struct Customer
{
	int customerID;
	std::string salutation;
	std::string dateOfBirth;
	std::string state;
	std::string creationDate;
};
struct Item
{
	int itemID;
	std::string size;
	std::string color;
	int manufacturerID;
	double price;
};
struct Order
{
	int orderItemID;
	std::string orderDate;
	std::string deliveryDate;
	Customer *customer;
	Item *item;
};
std::string SubStr(std::string str)
{
	int i = pos;
	std::string substr = "";
	while (str[i] != ';' && str[i]!='\0' && str[i]!=EOF)
	{
		substr += str[i++];
		pos++;
	}
	pos++;
	return substr;
}
int main()
{
	std::string str;
	std::ifstream file("C:\\Users\\User\\Desktop\\инт сис\\04\\orders_class.txt", std::ios::in);
	std::vector<Order*>*vec = new std::vector<Order*>();
	int strstr = 0;
	while (!file.eof())
	{
		//if (strstr == 50078)
		//	bool b = true;
		pos = 0;
		getline(file, str);
		if(!strstr)
			getline(file, str);
		//std::cout << str << std::endl;
		if (str != "")
		{

			vec->push_back(new Order());
			(*vec)[vec->size() - 1]->item = new Item();
			(*vec)[vec->size() - 1]->customer = new Customer();
			(*vec)[vec->size() - 1]->orderItemID = stoi(SubStr(str));
			(*vec)[vec->size() - 1]->orderDate = SubStr(str);
			(*vec)[vec->size() - 1]->deliveryDate = SubStr(str);
			(*vec)[vec->size() - 1]->item->itemID = stoi(SubStr(str));
			(*vec)[vec->size() - 1]->item->size = SubStr(str);
			(*vec)[vec->size() - 1]->item->color = SubStr(str);
			(*vec)[vec->size() - 1]->item->manufacturerID = stoi(SubStr(str));
			std::string::size_type sz;
			(*vec)[vec->size() - 1]->item->price = std::stod(SubStr(str), &sz);
			(*vec)[vec->size() - 1]->customer->customerID = stoi(SubStr(str));
			(*vec)[vec->size() - 1]->customer->salutation = SubStr(str);
			(*vec)[vec->size() - 1]->customer->dateOfBirth = SubStr(str);
			(*vec)[vec->size() - 1]->customer->state = SubStr(str);
			(*vec)[vec->size() - 1]->customer->creationDate = SubStr(str);
		}
		strstr++;
		
		//std::cout << strstr << std::endl;
	}
	file.close();
	system("pause");
	return 0;
}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы