Reading Tab delimited file into array

Nov 29, 2014 at 1:14am
I have a text file that I need to search records in. The records are saved line by line in this format ID NAME SCORE1 SCORE2 SCORE3 .... They are all separated by a tab space. What I am trying to do is to print the line that has been found as
ID : 123
NAME: asdhasj
SCORE1: 123

What I need to achieve is to store the results that have been found into an array and then format the data in the way I want it to be printed but I am not able to. Here is a part of my code so far, this just prints out the data segment by segment.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 while(infile.good())
  {
      getline(infile,line); 
      pos=line.find(search);
	  std::istringstream iss(line);
      std::string field;
	  
      if(pos!=string::npos) 

        while ( getline(iss, field, '\t') )
      {
         cout << " " << field << "\n";
      }
}
Nov 29, 2014 at 1:21am
I am working on very similar code at the moment.

Have you considered loading the file into memory? Have a struct or class defining the item, or record in your case.

It would be much easier manipulating data in memory than on a file itself. Could you give me an idea of what you want your entire program to do?
Nov 29, 2014 at 9:46am
@megatron My entire program needs to store game people and also search for a person's scores. I will look at at shoring into another file.
Nov 29, 2014 at 12:27pm
Ah right then, I think I can help. It may take a while for me to write out some outlaying code, bare with me.


Here we go, hope this helps.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <fstream>
#include <string>
#include <vector> // We will use this to store Players

struct Player // Define a "Player" data structure
{
	int ID;
	std::string Name;
	int Score1, Score2, Score3;
};

std::istream& operator>>(std::istream& is, Player& p)
{
	// In this function we will define how information is inputted into the player struct
	// std::cin is derived from the istream class
	is >> p.ID >> p.Name >> p.Score1 >> p.Score2 >> p.Score3;
	// When we have extracted all of our information, return the stream
	return is;
}

std::ostream& operator<<(std::ostream& os, Player& p)
{
	// Just like the istream, we define how a player struct is displayed when using std::cout
	os << "ID: " << p.ID << "\nName: " << p.Name << "\nScore1: " << p.Score1 << "\nScore2: " << p.Score2 << "\nScore3: " << p.Score3 << "\n";
	// When we have extracted all of our information, return the stream
	return os;
}

////////////////////////////////////////////////////////////////////////////////////////////

void Load(std::vector<Player>& r, std::string filename) // Taken from my latest code
{
	std::ifstream ifs(filename.c_str()); // Open the file name
	if(ifs)
	{
		while(ifs.good()) // While contents are left to be extracted
		{
			Player temp;
			ifs >> temp;  		// Extract record into a temp object
			r.push_back(temp);	// Copy it to the record database
		}
		std::cout << "Read " << r.size() << " records.\n\n";	
	}
	else
	{
		std::cout << "Could not open file.\n\n";
	}
}

void Read(std::vector<Player>& r) // Read record contents
{
	for(unsigned int i = 0; i < r.size(); i++)
		std::cout << r[i] << "\n";
}

void Search(std::vector<Player>& r, std::string n) // Search records for name
{
	std::cout << "Searching records for " << n << "\n\n";
	for(unsigned int i = 0; i < r.size(); i++)
	{
		if(r[i].Name.find(n) != std::string::npos)
			std::cout << r[i];
	}
}

int main()
{
	std::vector<Player> records;
	Load(records, "players.txt");
	Read(records);
	Search(records, "misheck");
	return 0;
}


players.txt:


01 Titan 10 20 30
02 misheck 30 50 80


OUTPUT:


Read 2 records.

ID: 1
Name: Titan
Score1: 10
Score2: 20
Score3: 30

ID: 2
Name: misheck
Score1: 30
Score2: 50
Score3: 80

Searching records for misheck

ID: 2
Name: misheck
Score1: 30
Score2: 50
Score3: 80
[Finished in 0.5s]


Please do not hesitate if you are stuck or struggling to understand something, I am more than happy to help.
Last edited on Nov 29, 2014 at 1:03pm
Nov 29, 2014 at 12:54pm
Thank you. I will wait to hear from you.
Nov 29, 2014 at 1:07pm
I updated my original post, let me know if you have any questions.
Nov 29, 2014 at 1:27pm
I will try it out in the next few hours. Thank you for your help. I have looked at the code on my phone and i think I will be able to understand
Nov 29, 2014 at 1:50pm
You are very welcome, if you run into any trouble let me know on here or PM me.

Good luck.
Topic archived. No new replies allowed.