0
#include<stdio.h>
#include<string.h>
#include<stdlib.h> 
int Search_in_File(char *fname,char *str){ 
    FILE *fp; 
    int line_num = 1; 
    int find_result = 0; 
    char temp[512]; 
    if((fp = fopen(fname, "r")) == NULL)
        return(-1);

    while(fgets(temp, 512, fp) != NULL){ 
        if((strstr(temp, str)) != NULL){ 
            printf("A match found on line: %d\n", line_num); 
            printf("\n%s\n", temp); 
            find_result++;
        }
        line_num++;
    } 
        if(find_result == 0){ 
            printf("\nSorry, couldn't find a match.\n");
        } 
        if(fp)
            fclose(fp);
    return(0);
} 
void main(){
    char file_name[15]; 
    char *fname; 
    *fname=file_name[15]; 
    char *str; 
    char string_to_be_searched[15]; 
    *str=string_to_be_searched[15]; 
    int result, errno; 
    printf("Enter The File Name :"); 
    scanf("%s",fname); 
    printf("Enter The String To Be Searched :"); 
    scanf("%s",str); 
    result = Search_in_File(fname , str); 
    if(result==-1){
        perror("Error"); 
        printf("Error number = %d\n", errno);
        exit(1); 
    }
}

I wrote this little program to search string from a file but it shows segmentation fault(core dumped) in gcc Linux What is this error for ? How can I fix it ?

Treycos
  • 7,373
  • 3
  • 24
  • 47
0xts
  • 2,411
  • 8
  • 16
  • Edit your question... you can use the return key to code – Treycos Oct 16 '16 at 20:43
  • @Treycos can you precise please ? – 0xts Oct 16 '16 at 20:45
  • Your code wasn't readable, i've edited – Treycos Oct 16 '16 at 20:46
  • @Treycos is it readable now ? – 0xts Oct 16 '16 at 20:52
  • there are only a couple of valid signatures for the `main()` function: All of them have a return type of `int`, never `void` – user3629249 Oct 18 '16 at 18:08
  • what are you expecting this line: `*fname=file_name[15];` to do? – user3629249 Oct 18 '16 at 18:10
  • in general, the handling of an error should be immediately after the thing that produced the error. Suggest moving these lines: ` if(result==-1){ perror("Error"); printf("Error number = %d\n", errno); exit(1); ` to immediately after the call to `fopen()` Note: the header file that contains the prototype for `exit()` also has the definition of `EXIT_FAILURE` which is a much clearer statement `exit( EXIT_FAILRUE)` than does `exit(1)` – user3629249 Oct 18 '16 at 18:13
  • when calling system functions, like `scanf()`, always check the returned value to assure the operation was successful. – user3629249 Oct 18 '16 at 18:14
  • when calling `scanf()` with a `%s` format specifier, always include a max character modifier that is one less than the length of the input buffer to avoid a buffer overflow (which would result in undefined behavior and can lead to a seg fault event. – user3629249 Oct 18 '16 at 18:16
  • suggest elimination of the variable `fname`. it just clutters the code. suggest: ``if( 1 != scanf( "%14s", file_name ) ) { // handle error } – user3629249 Oct 18 '16 at 18:18
  • what are you expecting this line to do: `*str=string_to_be_searched[15];`? – user3629249 Oct 18 '16 at 18:19
  • suggest elimination of the variable `str`. it just clutters the code. suggest: `if( 1 != scanf( "%14s", string_to_be_searched ) ) { // handle error } – user3629249 Oct 18 '16 at 18:21
  • for ease of readability and understanding: 1) follow the axiom: *only one statement per line and (at most) one variable declaration per statement.& 2) separate code blocks (for, if, else, while, do...while, switch, case,, default) via a blank line. – user3629249 Oct 18 '16 at 18:23
  • the posted code contains several 'magic' numbers. 'magic' numbers are numbers with no basis. I.E. 15, 512. Suggest using an `enum` statement or `#define` statements to give those 'magic' numbers meaningful names and using those meaningful names through out the code. – user3629249 Oct 18 '16 at 18:25
  • the function: `fgets()` includes the terminating newline character sequence (if available) So must always remove such terminating newline, if present) before making comparisons. Suggest using: `char *newline = NULL; if( NULL != ( newline = strstr( string, "\n" ) ) ) { *newline = '\0'; }` – user3629249 Oct 18 '16 at 18:31

2 Answers2

-1

This part of the code is causing the segmentation fault.

char file_name[15];
char *fname;
*fname=file_name[15];
char *str;
char string_to_be_searched[15];
*str= string_to_be_searched[15];

Your pointer needs to point to some address before you use it,

fname = file_name;

and

str= string_to_be_searched;

would take care of that.

So an appropriate code would be

char file_name[15];
char *fname;
fname=file_name;
char *str;
char string_to_be_searched[15];
str= string_to_be_searched;

Alternatively you could have also used malloc for assigning memory to your pointers

char *fname;
fname = (char*)malloc(sizeof(char)*15);
char *str;
str= (char*)malloc(sizeof(char)*15);
  • in C, the returned type from the heap memory allocation functions (malloc, calloc, realloc) is `void*`. such a type can be assigned to any other pointer. casting the returned type just clutters the code making the code more difficult to understand, debug, maintain. – user3629249 Oct 18 '16 at 18:37
-1
char *fname; 
*fname=file_name[15]; 

Think about what that code does. When you assign value to *fname, which memory does fname point to?

The answer is: fname is uninitialized pointer, which can point to any random location in memory. Most likely it contains 0, so trying to dereference it results in immediate SIGSEGV (but this is not guaranteed).

You should also learn to use a debugger, which would point you straight at the problem.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • actually, `fname` will contain the contents of the first byte past the end of the array `file_name[]` which could be anything in the range 0...255. – user3629249 Oct 18 '16 at 18:40