0

I am trying to take data from a CSV file containing statistics on every fixture from the 2013/2014 English Premier League season. I am trying to take the data that is valid to me so I can create a finalised league table from the results of the fixtures. Here is what two lines from the file look like:

2013-08-17,Arsenal,Aston Villa,1-3,1-1

2013-08-25,Cardiff,Man City,3-2,0-0

As you can see "2013-08-17" and "2013-08-25" are at index 0, "Arsenal" and "Cardiff" are at index 1 etc. I am using indexes 1 to 4 to help make my table and have started by splitting up each index at the comma so they print out as such:

Arsenal

Aston Villa

1-3

1-1

I have gone on to tokenise indexes 3 and 4 at the hyphen(which are "1-3" and "1-1" in the example above) so each number prints out on a separate line like this:

H:1

H:1

F:1

F:3

The numbers with "H:" in front are in the same pointer and the numbers with "F:" in front are all in another pointer, but I need the two numbers in H: to go in separate identifiers and the same for the numbers in F: and need that done for every fixture. I have been struggling to do that though and wondered if anyone could help?! Here is my function in which I am tokenising my data so you can see what's going on:

void tokenise(char *line)//the pointer line holds each line as the file as it is read in
{
    char homeTeam[32];
    char awayTeam[32];
    char fullScore[12];
    char halfTimeScore[12];


    char *token = strtok(line, ",");//the pointer *token is given the lines to hold
    int i = 0;

    while(token != NULL)
    {
        switch(i)
        {
            case 1:
                strcpy(homeTeam, token);//splitting indexes up at comma
                break;

            case 2:
                strcpy(awayTeam, token);
                break;

            case 3:
                strcpy(fullScore, token);
                break;


            case 4:
                strcpy(halfTimeScore, token);
                break;

            default:
                printf("");


        }

        i++;
        token = strtok(NULL, ",");



    }


    char *halfTimeScoreToken = strtok(halfTimeScore, "-");//*halfTimeScoreToken is the pointer that holds the numbers with "H:" in front

    while(halfTimeScoreToken != NULL)
    {
        printf("\nH:%s", halfTimeScoreToken);
        halfTimeScoreToken = strtok(NULL, "-");

    }

    char *fullScoreToken = strtok(fullScore, "-");//*fullScoreToken is the pointer that holds the numbers with "F:" in front

    while(fullScoreToken != NULL)
    {
        printf("F:%s\n", fullScoreToken);
        fullScoreToken = strtok(NULL, "-");

    }



}
MC93
  • 791
  • 6
  • 14
Aaron
  • 21
  • 5
  • "*struggling*" having "*separate*" variables (which you seem to call "*identifiers*") or "*struggling*" doing this for "*every fixture*"? – alk Aug 17 '17 at 10:43
  • Not great with terminology yet as still fairly new to programming, sorry! I am struggling to put every other number into separate variables. – Aaron Aug 17 '17 at 10:54
  • Ignoring the code, how did you get 1-3 and 1-1 (in that order) to turn into H:1, H:1, F:1, F:3 (in that order). Where's the logic? – Lundin Aug 17 '17 at 11:05
  • You might like to have a look into arrays. – alk Aug 17 '17 at 11:06
  • @Lundin: Parse out half-score after full-score and tokenise it before. :-) – alk Aug 17 '17 at 11:07
  • In the file, full time score comes before half time score( 1-3 is the full time score). I wanted to see it in chronological order for the sake of the stage I am on so swapped for now – Aaron Aug 17 '17 at 11:08
  • So when the game started, the score was 1-3. They then played the game backwards, removing balls from the net, ending up at 1-1 at half-time, before ending up at the inevitable 0-0. Whoever came up with this logic should stick to kicking on balls. – Lundin Aug 17 '17 at 11:17
  • I can't do anything on how they wrote the file. – Aaron Aug 17 '17 at 11:19
  • this line: `char *fullScoreToken = strtok(fullScore, "-");` will not work, as the extraction of `halfTimeScore` has already consumed the "-". Suggest: `fullScore = halfTimeScore + strlen( halfTimeScore +1;` also, final comma ',' was already replaced by a NUL character so looping to extract data will create problems in the final data contents, – user3629249 Aug 18 '17 at 17:10
  • please post your data structure. The code and example data does not seem to match the outputs shown – user3629249 Aug 18 '17 at 17:17

1 Answers1

0

Assume char *score points to a score like "1-3", then:

int i1= atoi(score);
while (*score!='-') score++;
score++;
int i2= atoi(score);

This is also called Stupid Character Processing.

A more complete example would be:

if (!isdigit(*score)) return(-1);
int i1= atoi(score);
while (*score && *score!='-') score++;
if (!*score) return(-1);
score++;
if (!isdigit(*score)) return(-1);
int i2= atoi(score);

where return(-1) means an error.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • If string didnt containt `-` you would access outside of bounds. Also [Why shouldnt i use atoi() ?](https://stackoverflow.com/questions/17710018/why-shouldnt-i-use-atoi) – kocica Aug 17 '17 at 10:54