reading from text file to a vector.

Hey all.

I am currently trying to write a program that sorts a vector based on columns. I am think i am all done, however i am currently adding the contents of the vector in the actual code as you can see on lines 31-33. What i want to do is to have the content in textfile input.txt. The reason is because the assignment i have has that as a rule.

So how do i add a text file into a vector so it has the same function as i already do in my code on lines 31-33.

input.txt
1
2
3
15 Femton XV
12 Tolv XII
122 Hundratjugotvå CXXII


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
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

struct Sortering
{
	// default constructor
	Sortering(int siffror, string bokstaver, string romerska) {
		this->siffror = siffror;
		this->bokstaver = bokstaver;
		this->romerska = romerska;
	}
	int siffror;
	string bokstaver;
	string romerska;
};

inline bool operator<(const Sortering& a, const Sortering& b)
{
	return a.siffror < b.siffror;
}

int main()
{

	vector<Sortering> vecSortering;

	vecSortering.push_back(Sortering(15,"Femton", "XV"));
	vecSortering.push_back(Sortering(12,"Tolv", "XII"));
	vecSortering.push_back(Sortering(122,"Hundratjugotvå", "CXXII"));

	sort(vecSortering.begin(),vecSortering.end());

	for(size_t i=0; i<vecSortering.size(); ++i)
		cout << vecSortering[i].siffror << ", " << vecSortering[i].bokstaver << ", "
 << vecSortering[i].romerska << endl;

	return 0;
}
Last edited on
Heh, funny how you can look for a solution so long and then add find it right after you post a thread.


Added this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	int tempsiffror;
	string tempbokstaver;
	string tempromerska;
	vector<Sortering> vecSortering;

	ifstream input("input.txt");
	if (input.is_open())
	{
			while (input >> tempsiffror >> tempbokstaver >> tempromerska)
			{
					vecSortering.push_back(Sortering(tempsiffror, tempbokstaver, tempromerska));
			}
	}
	else
	{
		cout << "Filen öppnades ej korrekt" << endl;
	}
something like this ?
Text.txt
1
2
3
4
10 "hi" "hey"
3 "done" "ok"
7 "done441d" "ok2"
2 "done121" "ok33"


main.cpp
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
47
48
49
50
51
52
53
54
55
56
#include <fstream>
#include <string>
#include <vector>
#include <iostream>

using std::ifstream;
using std::ios;
using std::string;
using std::vector;
using std::cout;
using std::endl;

struct Data
{
	int i;
	string s1;
	string s2;
};

bool CompareData(const Data& d1, const Data& d2)
{
	return d1.i < d2.i;
}
void PrintData(const Data& data)
{
	cout << data.i << "," << data.s1 << "," << data.s2 << endl;
}
template<typename T>void SortWhere(vector<T>& _array, bool(*_ptr)(const T&, const T&))
{
	sort(_array.begin(), _array.end(), _ptr);
}


auto main(int argc, char* argv[]) -> int
{
	vector<Data> container;
	ifstream reader("Text.txt", ios::in);
	if (reader.good())
	{
		while (!reader.eof())
		{
			Data data;
			reader >> data.i;
			reader >> data.s1;
			reader >> data.s2;
			container.push_back(data);
		}
	}
	SortWhere(container, CompareData);

	for each (Data current in container)
	{
		PrintData(current);
	}
	return 0;
}
something like this ?

No. Don't loop on eof(). It is an error-prone approach, for more than one reason.

You need to check the status of the file after reading it, not before.
Replace this:
1
2
3
4
5
6
7
8
    while (!reader.eof())
    {
        Data data;
        reader >> data.i;
        reader >> data.s1;
        reader >> data.s2;
        container.push_back(data);  // no idea whether the read succeeded, but process it anyway
    }


with this:
1
2
3
4
5
    Data data;
    while (reader >> data.i >> data.s1 >> data.s2)
    {
        container.push_back(data); // process only after the read was successful
    }

Here the body of the loop (the push_back) is executed only after a successful read from the file.

Last edited on
Topic archived. No new replies allowed.