.txt 파일을 읽는 변수가 있습니다. 이 파일은 다음과 같습니다C에서 문자열의 첫 번째 요소의 첫 번째 요소 가져 오기
Element1|Element2|Element3|...|ElementLast#Element1|Element2|Element3|...|ElementLast#Element1|Element2|Element3|...|ElementLast#Element1|Element2|Element3|...|ElementLast
가 지금은 싶은 일을 할
모든 'Element1이'를 얻을 그것이 특정 문자열에 동일한 경우는 확인이다.
나는 strtok()
함수에 대해 많이 읽었으며 사용하려고 시도했다.
이 제 기능입니다 : (ID는이 특별한 문자열, 나는 기능 중에 변환 할 수 있습니다.)
int PLUexists(int id) {
char idc[3];
sprintf(idc, "%d", id);
printf("%s", idc);
/* My Main String */
FILE *plu = fopen("kassa_plu.txt", "r");
char pluc[2000];
while(fgets(pluc, sizeof(pluc), plu) !=0);
/* My Token */
char *token;
/* get first line */
token = strtok(pluc, "#");
while(token != NULL) {
printf(" %s \n", token);
// Without that block, I get all lines seperated...
char t[255];
strcpy(t, token);
if (strcmp(strtok(t, "|"), idc) == 0) {
printf("Jep");
return 1;
}
// End 'block'
token = strtok(NULL, "#");
}
return 0;
}
지금은 단지 첫 번째 토큰을 인쇄 (그리고 그것을 반복)하면, 내 모든 라인을 얻을 (아래 참조)하지만 나는 또한 printf(" %s \n", token);
과 token = strtok(NULL, "#");
사이의 코드를 구문 분석 할 때 작동하지 않습니다. 그와
Element1|Element2|Element3|...|ElementLast
Element1|Element2|Element3|...|ElementLast
Element1|Element2|Element3|...|ElementLast
Element1|Element2|Element3|...|ElementLast
: 블록없이
Element1|Element2|Element3|...|ElementLast
Element2|Element3|...|ElementLast
Element3|...|ElementLast
...|ElementLast
ElementLast
당신은 "루프 strtok를"당신이 중첩 된 할 수 있도록'strtok_r'를 찾아 볼 수 있습니다 :
예를 들어, 여기
strchr()
에 따라 유사한 접근 방식이다. – immibis'while (flues (pluc, sizeof (pluc), plu)! = 0);'아무 것도하지 않고 세미콜론으로 EOF에 빠르게 도달합니다. 제거한 후 다음 코드 블록 주위에'{괄호}'가 필요합니까? –
strtok를 사용하는 이유 - strstr이 좋지 않습니까? – Hogan