I have a variable in which I read a .txt file. The file looks like this:
Element1|Element2|Element3|...|ElementLast#Element1|Element2|Element3|...|ElementLast#Element1|Element2|Element3|...|ElementLast#Element1|Element2|Element3|...|ElementLast
Now what I wanna do is, to get every 'Element1' and check if it is equal to an specific string.
I read much about the function strtok()
and I tried to use it.
This is my function: (id is this special string, I convert it during the function.)
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;
}
Now if I just print out the first token (and repeat it), I get all of my lines seperated (see underneath) but when I also parse the code between the printf(" %s \n", token);
and token = strtok(NULL, "#");
it won't work.
Without the block:
Element1|Element2|Element3|...|ElementLast
Element1|Element2|Element3|...|ElementLast
Element1|Element2|Element3|...|ElementLast
Element1|Element2|Element3|...|ElementLast
With it:
Element1|Element2|Element3|...|ElementLast
Element2|Element3|...|ElementLast
Element3|...|ElementLast
...|ElementLast
ElementLast