Question

I try to cat a string that type in char* in c++ this is my code

void DBManager::printMatched(char* line, char* fileName)
{
    line = strtok(line,"Show");
    char* teamAName = strtok(line," ");
    char* teamACity = strtok(NULL,"-");
    char* teamBName = strtok(NULL," ");
    char* teamBCity = strtok(NULL,"\n");
}

and this is the text in line "Show abcde fghij - klmnop qrstu"

this is the variable data:

 teamAName = abcde
 teamACity = fghij
 teamBName = klmnop
 teamBCity = qrs

how can i fix teamBCity i need to cut to the in of the row.

i work on Linux system.

Was it helpful?

Solution

I think this is what you want. Not entirely sure given your example. It can obviously be improved upon and maybe you want to use more c++ish facilities?

void DBManager::printMatched(char* line, char* fileName)
{
    char* linecpy   = strdup(line);

    char* dummy     = strtok(linecpy," ");
    char* teamAName = strtok(NULL," ");
    char* teamACity = strtok(NULL," ");
          dummy     = strtok(NULL," ");
    char* teamBName = strtok(NULL," ");
    char* teamBCity = strtok(NULL," \n");

    printf("teamAName %s\n", teamAName);
    printf("teamACity %s\n", teamACity);
    printf("teamBName %s\n", teamBName);
    printf("teamBCity %s\n", teamBCity);

    // do something with strings here?
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top