문제

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.

도움이 되었습니까?

해결책

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?
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top