0

i am having a directory which contains 4 files namely 1.c,2.c,3.c and 4.c.i am reading the file names present under this directory by using readdir system call which returns to some structure variable namely myStruct.

2)I am having another open file namely a.txt file which contains file names like 1.c,2.c,3.c,4.c etc...

My intention is to compare the files present in a.txt with the files presen in the directory(just the name comparison is enough..not checking its contents).

when i do the comparison,even though the names present in the directory matches with those present in the a.txt file,they dont show equal comparison and then when i printed the lenghths they are unequal.

Can anyone please let me know any solution to this problem

thanks maddy

maddy
  • 21
  • 1
  • 4
  • 6
    Can you post your code? – Fred Larson Jun 20 '11 at 15:59
  • 1
    How are you reading the names from the input file (a.txt)? Are you keeping the `'\n'` before the comparison, perhaps?? `"1.c"` won't ever compare equal to `"1.c\n"` **Some code would help us help you** ... and welcome to SO :) – pmg Jun 20 '11 at 16:00
  • It would help if you showed us some of your program code. Do the strings in your file a.txt show themselves one or two characters longer than the directory entries? The extra one or two characters could be ```"\n"``` or ```"\r\n"```. – Heath Hunnicutt Jun 20 '11 at 16:00
  • "they dont show equal comparison" - just to check, you are using `strcmp` or similar on the two `entry->d_name` s, right, not something else? – Rup Jun 20 '11 at 16:01
  • i i m not in a position to post the code now.Just some logic would be really great help – maddy Jun 20 '11 at 16:04
  • @Health...i m not sure what it shows..but the length of the strings taken from the a.txt and from the directory are different even though the string names are equal – maddy Jun 20 '11 at 16:07

1 Answers1

1

When you read from the file, there is an extra null character at the end of the line you have read, so the comparison will show that they are unequal. So after reading the line, trim off the \n and then try.

EDIT

This discussion tells you about how to trim whitespaces in a string using C - Painless way to trim leading/trailing whitespace in C?

Community
  • 1
  • 1
Balanivash
  • 6,709
  • 9
  • 32
  • 48