File Search String (C)

So my code is not working ...

test.c:27: warning: passing argument 1 of ‘search’ from incompatible pointer type

which is a line of fgets.

My code opens the file, reads the file line by line, and I'm trying to create a "search" function that will return a value indicating whether this line is found in this line of the file.

My ultimate goal is to seek and replace a program. But one step at a time, huh? this is what i have so far:

#include <stdio.h>
#include <string.h>

int search(const char *content[], const char *search_term)
{
    int t;

    for(t=0; content[t]; ++t){
        if(!strcmp(content[t], search_term)){
            return t; // found
        }
    }
    return 0; // not found
}


int main(int argc, char *argv[])
{
    FILE *file;
    char line[BUFSIZ];
    int linenumber=0;
    char term[20] = "hello world";

    file = fopen(argv[1], "r");
    if(file != NULL){
        while(fgets(line, sizeof(line), file)){
            if(search(line, term) != -1){
                printf("Search Term Found!!\n");
            }
            ++linenumber;
        }
    }       
    else{
        perror(argv[1]); 
    }

    fclose(file);
    return 0;
}
+3
source share
6 answers

Edit

int search(const char *content[], const char *search_term)

to

int search(const char content[], const char *search_term)

EDIT:

Also change:

if(!strcmp(content[t], search_term)){

to

if(!strcmp(&content[t], search_term)){

or

if(!strcmp(content + t, search_term)){

Since you use strcmpto find a match, you cannot find all occurrences of the search string in the file. You will find only those lines that end in search_string.

: "hello world" , 2:

I wrote hello world
hello world is good

, .

, :

, fgets, , , :

while(fgets(line, sizeof(line), file)){
  line[strlen(line)-1] = 0;

, 0, -1.

+3

, libstr() libc .

+2

:

const char *content[]

. :

const char *content  

/

#include <stdio.h>
#include <string.h>

int search(const char *content, const char *search_term)
{
    int t;

    for(t=0; content+t; ++t){
        if(!strcmp(content[t], search_term)){
            return t; // found
        }
    }
    return 0; // not found
}


int main(int argc, char *argv[])
{
    FILE *file;
    char line[BUFSIZ];
    int linenumber=0;
    char term[20] = "hello world";

    file = fopen(argv[1], "r");
    if(file != NULL){
        while(fgets(line, sizeof(line), file)){
            if(search((const char*)line, term) != -1){
                printf("Search Term Found!!\n");
            }
            ++linenumber;
        }
    }       
    else{
        perror(argv[1]); 
    }

    fclose(file);
    return 0;
}
+2

:

int search(const char *content, const char *search_term)
+1
  • , .
  • strcmp, content[t]. content[t] - , .
  • , . return 0; - return -1?

  • ... , strncmp, .

.

int search(char *content, const char *search_term)
{
    int t;

    for(t=0; content[t]; ++t){
        if(!strncmp(&content[t], search_term, strlen(search_term))){
            return t; // found
        }
    }
    return -1; // not found
}
+1

:

int search(const char *content, const char *search_term)
{
   char *result = strstr(content, search_term);
   if(result == NULL) return 0;
   return (int)(result - content);
}

result content - , . , , ?

, . 0 , , 0. -1, :

if(result == NULL) return -1;

Further, however, you can see how this method is just a wrapper for functionality that you do not even use later. If all you care about mainis whether the string found, just completely remove this function and write mainas follows:

int main(int argc, char *argv[])
{
   FILE *file;
   char line[BUFSIZ];
   int linenumber = 1;
   char term[20] = "hello world";

   file = fopen(argv[1], "r");
   if(file != NULL) {
      while(fgets(line, sizeof(line), file)){
         if(strstr(line, term) != NULL) {
             printf("Search Term Found at line %d!\n", linenumber);
         }
         ++linenumber;
      }
   }
   else {
      perror(argv[1]);
   }

   fclose(file);
   return 0;
}
0
source

Source: https://habr.com/ru/post/1738332/


All Articles