-2

I'm writing some code to read a text file on stdin using fgets which compares two adjacent lines from the 1st and 2nd all the way to the 2nd last and last for equality. My problem is that it works all the way till the final case.

For example let's say our text file contains the following:

one
two
three
three
four
four

When I use strcmp on the last 2 lines, -1 is returned and when I use strlen on both 'four's, I got a 5 and a 6 respectively.

So where are the extra characters coming from and how can I overcome that so I can do a proper string comparison and have strcmp return a 0? Any tips would be great!

Calahan
  • 35
  • 6

1 Answers1

0

Have you tryed removing '\n' char inside string saved after read each line?

fgets(line, SIZE, FILE) // Save line readed inside var line

line[strcspn(line, "\n")] = '\0'; // Remove '\n' inside stored var

Removing '\n' char should solve your problem.

JuMoGar
  • 1,740
  • 2
  • 19
  • 46
  • I have but the gap is still there, I'm still figuring out what's lacking in the last line. – Calahan Apr 07 '18 at 13:07
  • update: Sorry for the stupid question, all I had to do was concatenate a carriage return and a newline in the strcmp statement, since 13 and 10 are ASCII values for those. Anyways thanks for the answer! – Calahan Apr 07 '18 at 13:18
  • Ok, no problem. I'm glad you found the answer. I recommend that you answer your same question, posting the answer you have found, help with other people with your problem. – JuMoGar Apr 07 '18 at 13:21