The file:
Code:
id:name:type:ssid:secret
---------------------
00:Temp1:1:Terminus:1sdfsdfdsffdfdfffssffdsfssfsdfdsfdsffdsfd86364aefe6f5f57705c4dd36e
01:Temp2:1:Something:sdkfjr8sjre7sdjfc42755sdfsdfc367c8e68sd8fsdfdshjsd8878dfjd87dsjd
The code:
Code:
char line[128];
while (fgets(line, sizeof line, fp) != NULL && (strcmp(strndup(line, 2), "00") == 0))
printf("%s", line);
}
Yelds nothing.
However, this code:
Code:
while (fgets(line, sizeof line, fp) != NULL && (strcmp(strndup(line, 2), "id") == 0))
Yelds the line: id:name:type:ssid:secret
The way a read the while-line is:
- If the line is NOT NULL (i.e., there IS actually a line), AND the first two characters in this same line equals "00" (or "id"), then print the line.
But how come it "works" when searching for the two first characters in the first line, but not when searching for the two first characters in the other lines? (EDIT: It doesn't work, really, because if I copy the first line to the end of the file, it still only finds the first.)
Can someone please explain why I'm seeing this all wrong? And perhaps tell me how to write the while-sentence so that it gets the lines while they're not ZERO AND the first two characters equals (f.ex.) "02"? I.e.: Search for one or more lines starting with two specific characters. Or is it better to just check the lines inside the while-loop and break; out of it if there's a match? Like this:
Code:
while (fgets(line, sizeof line, fp) != NULL) {
if (strcmp(strndup(line, 2), "00") == 0) {
printf("%s\n", line);
break;
}
}