How can I search for a specific character in a char array ?
Follow my code, but I think it’s not correct in the function strchr:
while((c = getc(fp)) != EOF) {
for (i = 0; i < 1; i++) {
c2[i] = c;
int test = strchr(";", c2[i]);
}
printf("%c", c);
}
I have a structure that has int index, int data, and a pointer to the next register. I fill an array (c2[100]) with some data that come from my .cvs file. In the first register of my array I got something like this: 800;lucas . I need to find the character ‘;’ in this array and split it, and then the number 800 will be the structure->index and the name ‘lucas’ will be the structure->data.
For each character that is read, you are storing it into the first slot of your
c2[]array (ignoring the rest of the array), and then callingstrchr()to check if the read character is a;or not. Usingstrchr()for that is overkill. The following would be much simplier:If you are actually trying to search your array instead, then you are using
strchr()the wrong way. It should be more like this instead, assumingc2[]already contains the null-terminated string data you want to search in: