Reading "complex" data from .txt file : revisited

Pages: 12
> I now have it and i will go through all bits of the book to keep myself abreast with C++.

It is still a very good book, go through it to understand the concepts that it presents.

However, it is very dated (2001); for instance, much of the material on 'Object Factories' has been thoroughly obsoleted with the advent of variadic templates, rvalue references and perfect forwarding.
yes, i see it is very dated. Do you have a more recent one to recommend? I currently read C++ Primer,5th ed (Lippman SB et al, 2013) and C++, How to program 8th Ed by Deitel
Perhaps 'The C++ Standard Library: A Tutorial and Reference (2nd Edition)' by Josuttis would be a good companion book for C++ Primer.
https://www.amazon.com/dp/0321623215
Thanks JLBorges for reference. Table of contents looks rich. I will definitely grab one.

UPDATE [19-05-2017]: This book is VERY good. Borrowed one from the library and read chapter 7.8 (since i badly need to understand maps better for my current project). I can't believe how much i have been missing.

============================================================
I don't know if i have to create another thread but i have another similar(but seemingly trivial case)

Case description:
There is a file containing data about trains in a company. The file is arranged as follows


trainId depStation arrStation depTime arrTime maxSpeed type type type type 
trainId depStation arrStation depTime arrTime maxSpeed type type type type type 

// sample data in file.txt
1 MaxCentral Milan 22:17 23:35 171 4 1 1 4 // 4 types : electric, bed, bed, electric
2 Mall2 Market 19:01 19:39 170 5 3 3 3 5 // 5 types diesel, closed, closed, closed, diesel
3 Station1 Station2 06:30 06:58 177 5 2 2 5 // 4 types

//types again
enum VehicleType{SEAT_WAGON = 0, BED_WAGON, OPEN_WAGON, CLOSED_WAGON, ELECTRIC_ENGINE, DIESEL_ENGINE};


This is my try
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
ifstream readTrain(TRAIN_FILE);
if (!readTrain.is_open())
{
	throw runtime_error("Error opening trains data file");
}
else
{
	//train id, deptStation, destination, deptTime, arrivalTime, maxSpeed
	string line, id, dStation, aStation, dTime, aTime;
	double mSpeed = 0;

	// various types of vehicles attached to the train
	int typ = 0;

	while (getline(readTrain, line))
	{
		istringstream iss(line);
		iss >> id >> dStation >> aStation >> dTime >> aTime >> mSpeed;
		while (iss >> typ) // trying to read the rest of the line. MY PROBLEM
		{
			VehicleType type = static_cast<VehicleType>(typ);
			vehType.push_back(type);
			trVeh.insert({id, vehType});  // map<string(trainID), vector<VehicleType>>
		}
		int d = intFromStringTime(dTime), ar = intFromStringTime(aTime);
		double sp = mSpeed;

		//void Train::setValues(const string & pid, string dep, string arr, int dTime, int aTime, double mSpeed)
		setValues(id, dStation, aStation, d, ar, sp);

		// make train object
		//Train(string id, Destination d, int dTime, int aTime, double mSpeed, vector<VehicleType> types)
		// Destination(a class) holds depature and arrival stations
		Train t(id, spur, d, ar, sp, vehType);

		// push it into trains vector
		trains.push_back(t);
	}
}


Issue is that there can exist 4 kinds of wagons(Vehicles as i call them) or 5 in a train. I need the magic to know when there are 4 or five.

Thanks in advance for your input
Last edited on
SOLVED

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
ifstream readTrain(TRAIN_FILE);
if (!readTrain.is_open())
{
	throw runtime_error("Error opening trains data file");
}
else
{
	//train id, deptStation, destination, deptTime, arrivalTime, maxSpeed
	string line, id, dStation, aStation, dTime, aTime;
	double mSpeed = 0;

	// various types of vehicles attached to the train
	int typ = 0;

	while (getline(readTrain, line))
	{
		istringstream iss(line);
		iss >> id >> dStation >> aStation >> dTime >> aTime >> mSpeed;
		while (iss >> typ) // trying to read the rest of the line. MY PROBLEM
		{
			VehicleType type = static_cast<VehicleType>(typ);
			vehType.push_back(type);
			//trVeh.insert({id, vehType});  // map<string(trainID), vector<VehicleType>>
		}

                trVeh.insert({id, vehType});  // map<string(trainID), vector<VehicleType>>
                vehType.clear(); // make an anew vehType

		int d = intFromStringTime(dTime), ar = intFromStringTime(aTime);
		double sp = mSpeed;

		//void Train::setValues(const string & pid, string dep, string arr, int dTime, int aTime, double mSpeed)
		setValues(id, dStation, aStation, d, ar, sp);

		// make train object
		//Train(string id, Destination d, int dTime, int aTime, double mSpeed, vector<VehicleType> types)
		// Destination(a class) holds depature and arrival stations
		Train t(id, spur, d, ar, sp, vehType);

		// push it into trains vector
		trains.push_back(t);
	}
}
Topic archived. No new replies allowed.
Pages: 12