2

I am writing a program to compare two files. If matching lines occur then the program will continue to do some task. My second file has only one line and the first file has several lines

Contents of File_1

apple is red
oranges are orange 
banana is yellow
cat is black
red is not green

Contents of File_2

cat is black

I have used fscanf function to read the lines of File_2 and store it in a variable.

if ((fp=fopen(File_2, "r")) == NULL) 
{
  printf("Error opening File");
}
fscanf(fp,"%[^\n]", name);
fclose(fp);

I have used the following method to search for similarities in File_1

 fp = fopen(File_1, "r");
      while ((read = getline(&line, &len, fp)) != -1)
          {
             if (strcmp(line,name)==0)
               {
                printf("Hurray\n");
                break;
               }
             else
               {
               printf("I am unlucky\n");
               }
          }
      fclose(fp);

But my issue is that,

strcmp() is not returning 0

I was wondering what went wrong here. Any suggestions would be appreciated.

Nayantara Jeyaraj
  • 2,624
  • 7
  • 34
  • 63
John P Raj
  • 33
  • 6
  • 2
    Welcome to SO! Did you try printing out the strings to see if there is a trailing newline on one or any extra characters? – ggorlen May 23 '19 at 04:21
  • 1
    When debugging problems like this, it helps to print the strings (as already suggested). Be sure to put delimiters before and after the string so that you can see any leading or trailing whitespace. For example: `printf("<%s>\n<%s>\n", line, name)` – user3386109 May 23 '19 at 04:25
  • 1
    You think this part of the [man page of `getline`](http://man7.org/linux/man-pages/man3/getline.3.html) may have something to do with it? : *"The buffer is null-terminated and **includes the newline character**"*. The name you're reading obviously won't, you're using set-formatting to purposely exclude it. – WhozCraig May 23 '19 at 04:39
  • `printf("Error opening File");` - Surely after this line you need a return or exit – Ed Heal May 23 '19 at 08:41

2 Answers2

1

I have managed to fix it. Dropped the new line character which was included in getline() using the following way:

fp = fopen(File_1, "r");
      while ((read = getline(&line, &len, fp)) != -1)
          {
             line[strcspn ( line, "\n" )] = '\0';   \\ will drop the newline character
             if (strcmp(line,name)==0)
               {
                printf("Hurray\n");
                break;
               }
             else
               {
               printf("I am unlucky\n");
               }
          }
      fclose(fp);

I hope there are better ways to do it.

Thanks ggorlen, user3386109 and WhozCraig for the tips in debugging. Also for the edit by Nayantara Jeyaraj.

John P Raj
  • 33
  • 6
0

when you are using getline to read File_1 you are getting the \n newline character as WhozCraig pointed out.

The following to terminate the lines with \0 should fix this issue:

      while ((readlen = getline(&line, &len, fp)) != -1)
          {
             if (line[readlen-1] == '\n')
                 line[--readlen] = '\0';
             if (strcmp(line,name)==0)
runwuf
  • 1,701
  • 1
  • 8
  • 15