0

C program for binary search

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

typedef struct node {   //LINKED LIST FUNCTION
        char *words;
         struct node *next;
                } Node;

//reads single words from file also allocate needed space.

    char *strdup (const char *s) {
            char *d = malloc (strlen (s));
                if (d == NULL) return NULL;
                    strcpy (d,s);
                     return d;
           }
     int main (int argc, char *argv[])
    {
    FILE *file1;
    FILE *file2;
    FILE *file3;

    file1 = fopen (argv[1],"r");
    file2 = fopen (argv[2],"r");


    char letters [100];
    char tempLetters [100][50]; 

//I tempLetters FOR BINARY SEARCH it has Words stored in it

 Node *current , *head;
    int check;
    head = current = NULL;
    int i = 0;

 while (! feof (file1)){
            fscanf (file1,"%s",letters);
            Node * list = malloc (sizeof (Node));
            list -> words = strdup(letters);
            list -> next = NULL;
    if(head == NULL){
        current = head = list;
    } else {
        current = current->next = list;
    }
    // printf ("%s\n", current->words);
            strcpy (tempLetters[i],current -> words);

//HERE I STORED Words in tempLetters

    i++; 

//i++ is for Keeping track of how many words. }

    // FILE 2 reading queries:
    char query [100]; //Just a local variable 
    int q = 0;
    char QArray [100][50];//NEED THIS ONE FROM BINARY SEARCH
    while (!feof (file2)){
            fscanf (file2, "%s", query);
    if (!feof (file2)){

    //Keeping track of how many words
    strcpy  (QArray[q], query);
             q++;  
       }
        }

} /* //Binary Search I want to read single words from QArray and find that if there is a same word in temLetters array. and If there is then print that word and also print its ARRAY index. If there is not then read next word from QArray till QArray == NULL. I AM HAVING TROUBLE PASSING THOSE TOO ARRAYS in function. */

    fclose (file1);
    fclose (file2);

    return 0;}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • `char *d = malloc (strlen (s));` is wrong. You need `char *d = malloc (strlen (s) + 1);`. That needs to be fixed first. It may or may not resolve other errors. – R Sahu Jul 02 '16 at 06:18
  • If i keep that +1 it prints and "Enter" from file so i removed that. – Apurva Patel Jul 02 '16 at 06:21
  • Look at the answers to [strdup() - what does it do in C?](http://stackoverflow.com/questions/252782/strdup-what-does-it-do-in-c). – R Sahu Jul 02 '16 at 06:33
  • I don't know why but it stop printing that enter when i removed it. Anyway i got it running and thank you for your time :D This site is really helpful YES! – Apurva Patel Jul 02 '16 at 06:47

1 Answers1

0

char *d = malloc (strlen (s)); is wrong. You need char *d = malloc (strlen (s) + 1);. That needs to be fixed first. It may or may not resolve other errors. – R Sahu

Armali
  • 18,255
  • 14
  • 57
  • 171