Comparing 2 files problem ?

hey guys
I have this problem, I have a text data file “teams.dat” that contains information regarding the european’s football teams. The structure of the file is:
GER Germany
FRA France
POL Poland
ITA Italy
ENG England
and another text data file named “score.dat” that contains the scores of the first round of the Euro Cup 2012. The file have the following structure:
GER 1 POL 0
FRA 1 ENG 1
ITA 2 SPA 1
The problem I need to Read the data from the “score.dat” file and print on the screen the “country Name” (not the “Country Code”such as GER,FRA ect..) of the winning teams. You will find the “Country Name” in the “teams.dat” file.
This is where I am stuck:
#include<iostream>
#include<fstream>
using namespace std;
struct teams{
char country[20];
char code1[3];
};
struct scores{

char code[3];
char code2[3];
int score;
int score2;
};
int main(){

ifstream inf;
teams t;
scores s;
inf.open("teams.dat",ios::in);
ifstream inf2("score.dat",ios::in);
while(inf2>>s.code>>s.score>>s.code2>>s.score2){
if(s.score>s.score2) { HERE is where I am STUCK I need to Read the data from the “score.dat” file and print on the screen the “country Name” (not the “Country Code”) of the winning teams. You will find the “Country Name” in the “teams.dat” file.
Any help is appreciated :)
Last edited on
Hi Tapor,

Welcome to cplusplus forum.

Firstly, please use the code and quote tags - it make things easier to read. They are found on the right of where you write your post. The <> button is for code and " is for quoting people or stuff in files.

A big thing in programming is to make sure that you initialise things before using them. you have declared the structs, but not assigned anything into them.

The fact that you have files of info, tells me that you need an array of these structs. Use a loop to read the files and put the relevant info into the array of structs.

The info is in files, but you are asking for input then don't read it in with anything.

You have specified ios::binary are sure thats what you need?

A good thing to do, is write your methodology in the file as comments. This will help you sort out how you are going to do the problem.

Then write the code after the comments. The comments serve as documentation in the code.

There are other problems, read up on how to deal with file streams.

Hope this helps

TheIdeasMan
Fully object oriented...you have to buy me a beer :) ... speaking of football...makes me feel thirsty.

map is what you need - to make "mappings" .. it's a dictionary ...
code = country

then you can use a vector of matches if you want to store them for later use.

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
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
using namespace std;

//country mapping <code, full country name>
map<string, string> countries; 

struct Match
{
	string country1;
	unsigned int result1;
	string country2;
	unsigned int result2;
};

int main()
{
	vector<Match> matches;
	Match match;

	string code;
	string country;	

	ifstream teamsFile("teams.dat",ios::in);
	ifstream scoresFile("score.dat",ios::in);

	while(teamsFile>>code>>country)
		countries[code] = country;

	while(scoresFile>>match.country1>>match.result1>>match.country2>>match.result2)
	{
		//if you want to store all matches for later use
		//matches.push_back(match);

		if(match.result1 > match.result2)
			cout<<countries[match.country1]<<" won against "<<countries[match.country2]
				<<" with "<<match.result1<<" - "<<match.result2<<endl;
		else
			if(match.result1 < match.result2)
				cout<<countries[match.country2]<<" won against "<<countries[match.country1]
					<<" with "<<match.result2<<" - "<<match.result1<<endl;
			else
				cout<<countries[match.country1]<<" drew "<<countries[match.country2]
					<<" with "<<match.result1<<" - "<<match.result2<<endl;
	}

	scoresFile.close();
	teamsFile.close();

	return 0;
}
You have specified ios::binary are sure thats what you need?

ios::in does not imply ios::binary;

First thing, country code needs to be four characters long to make room for the null terminator.

A program should run like this:
1) input
2) compute
3) output

You have all of these things happening at once, and it makes it tough for everyone involved. You might need to group 2 and 3 together for the output, but in general, input should be done and done with.

After doing input, you should have an array of "teams" and an array of "scores".

To figure out how to match the code with the team name, use strcmp(). Something like:
1
2
3
4
5
6
7
8
9
10
for (int i = 0; i < amountOfScores; i++) // go through the array of scores
{
  for (int j = 0; j < amountOfTeams; j++) // go through the teams
  {
    if (strcmp(scoreArray[i].code, teamArray[j].code) == 0) // strcmp returns 0 for a match
    {
      cout <<  teamArray[j].country << endl;
    }
  }
}


I think will work to at least connect a code from the file with a team.

Edit: btw, keep track of the amount of structs you make when you read the file :).
Last edited on

@LowestOne
ios::in does not imply ios::binary;


The OP must have edited his post - I don't see it any more. And knuth didn't have it in his code either.

I am not sure, but I thought that one doesn't need ios::binary if it's an ordinary text file?
The OP must have edited his post
Hmm, okay.
Right, just like you don't need to add ios::in on an ifstream object.

As far as I can tell, the main reason to open a file in binary is when that file contains unprintable characters.

If you have a file that goes:

[BYTE] length of string,
[BYTE] one for each letter in string

Then you have real trouble when your string length is 4 and you aren't reading in binary. 4 is treated as EOT, and will set the eof bit in the ifstream. (I think that's how it works)
I thought the purpose of binary files was to deal with binary file in a particular format. When you read from the binary file you need to know th format.

As an example, say I wrote a string, a double, and an unsigned to a binary file. If you wanted to read that file you would need to know how many bits each of these things were, so you could read and convert them properly. Floating point number are often written in binary because they take up a lot less room in that format.

The functions read() and write() will read/write unformated data regardless of the openmode.

You need to know the format of the file no matter what. If your reading formatted data (ifstream's >>) then you need to be sure you're reading an double rather than a string. Sure, binary data saves space, but I think the main thing is the ease of reading from the file.

A text file would look like this:
this is a string|123.456|This is another string

so now you need to know what your delimiter is. You also need escape characters in case the string has the delimiter in it. In the case of a string, you're reading the file byte at a time looking for these things. Check out the .csv format.

Much easier to know that the first 16 Bytes are a string, the next 4 bytes are a double and the next bytes are another string.

Last edited on
Thank you guys for all of the help and knuth (37) I have never learned mapping, I cannot use it for my assignment anyways thank u, Plus I didn't check again for any replies until now, I never had any problems with C++ assignments before I just had a mind block and I really wanted to solve it myself, went to bed the next day I woke up and knew how to solve it, turns out I forgot to use function clear()( like inf2.clear()) ; I still don't know what the function clear(), I saw it in my notes and said heck try it I tried everything else and it worked :) anyways thank you guys for the help.
thankx guys for your help and suggestions..
http://dissertationwritinghelp.blogspot.com/
Topic archived. No new replies allowed.