问:在 C 中比较字段 (Libfdr) 和数组
Q: Compare Fields (Libfdr) and Array in C
首先,我是初学者。对不起,如果这是一个超级简单的问题。我想查看用户提供的c文件中的一些函数并打印出来
Linux 中的用户输入:$ ./question2 -s pro1.c
终端打印:Counter: 0
此代码无法比较。
int main(int argc, char *argv[2])
{
parameter = malloc(sizeof(char) * 2);
if (argc >= 2)
{
parameter = argv[1];
IS is; // #include "fields.h"
char *functions[5] = {"strcat", "strcpy", "sprintf", "gets", "getpw"};
is = new_inputstruct(argv[2]);
int i, counter;
while (get_line(is) >= 0)
{
//printf("%s\n", *is->fields);
for (i = 0; is->fields[i] != NULL; i++)
{
if (strcmp(is->fields[i], "strcpy") == 0) // if true
{
printf("%s\n", is->fields[i]);
counter++;
}
}
}
printf("Counter: %d\n", counter);
您可以使用带有 strstr 函数的比较 2 字符串。
if(strstr(is->fields[i],"strcpy")){
printf("%s\n", is->fields[i]);
counter++;
}
首先,我是初学者。对不起,如果这是一个超级简单的问题。我想查看用户提供的c文件中的一些函数并打印出来
Linux 中的用户输入:$ ./question2 -s pro1.c
终端打印:Counter: 0
此代码无法比较。
int main(int argc, char *argv[2])
{
parameter = malloc(sizeof(char) * 2);
if (argc >= 2)
{
parameter = argv[1];
IS is; // #include "fields.h"
char *functions[5] = {"strcat", "strcpy", "sprintf", "gets", "getpw"};
is = new_inputstruct(argv[2]);
int i, counter;
while (get_line(is) >= 0)
{
//printf("%s\n", *is->fields);
for (i = 0; is->fields[i] != NULL; i++)
{
if (strcmp(is->fields[i], "strcpy") == 0) // if true
{
printf("%s\n", is->fields[i]);
counter++;
}
}
}
printf("Counter: %d\n", counter);
您可以使用带有 strstr 函数的比较 2 字符串。
if(strstr(is->fields[i],"strcpy")){
printf("%s\n", is->fields[i]);
counter++;
}