当通过命令行给出参数以搜索文件中由文件中的某个分隔符分隔的字符串时,我们在 C 中使用什么命令/代码?
char searchString[20];
char *src;
f = fopen("passwd", "r");
if (f == NULL)
{
printf("Error while opening the file");
//exit(1);
}
while (fgets(ch, sizeof ch, f)!= NULL)
{
/*printf("%s\n", ch); */
for (j = 1, str = ch; ; j++, str= NULL)
{
char *token = strtok_r(str, ": ", &saveptr);
if (token == NULL)
break;
//printf("%s---\n---", token);
//printf("%s",token);
//printf("%s",strlen(token));
if (strstr(token, argv[2]) != NULL)
{
printf(token);
}
}
//src = strstr(token, argv[2]);
//printf ("String matched");
}
fclose(f);