Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Unix C programming for finding file

Reply
Thread Tools

Unix C programming for finding file

 
 
dawn
Guest
Posts: n/a
 
      11-24-2004
Hi all,
I am now working on a C program under Unix.
The requirement for the program is that:
A file name is passed to program as a parameter. The program will
Find files under a specified directory. The matched file must have the
same content with the given file. It does not matter whether the
filenames are the same.

It is easy to find file that has the same name with given file, but
may be hard to find the files that with the same content. In my
knowledge, i give two solutions:

1) Go throught the directory and its sub direcotry tree, and when
meeting a file, Use the stand C library function to open that file and
the given file, and then compare those contents in buffers to see if
they are the same.

2) Go through the directory and its sub deirectroy tree, and when
meeting a file, execute the system shell command "diff" to compare the
two files to see if they are the same in content.


The two solutions seems not very smart and they are very running
slowly. I wonder if there is any library function that can compare two
file contents just like "strcmp" to compare two string.
Or maybe there are some other smart ways to achieve it.

Thank you for suggestions.
 
Reply With Quote
 
 
 
 
dandelion
Guest
Posts: n/a
 
      11-24-2004

"dawn" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi all,
> I am now working on a C program under Unix.
> The requirement for the program is that:
> A file name is passed to program as a parameter. The program will
> Find files under a specified directory. The matched file must have the
> same content with the given file. It does not matter whether the
> filenames are the same.
>
> It is easy to find file that has the same name with given file, but
> may be hard to find the files that with the same content. In my
> knowledge, i give two solutions:
>
> 1) Go throught the directory and its sub direcotry tree, and when
> meeting a file, Use the stand C library function to open that file and
> the given file, and then compare those contents in buffers to see if
> they are the same.
>
> 2) Go through the directory and its sub deirectroy tree, and when
> meeting a file, execute the system shell command "diff" to compare the
> two files to see if they are the same in content.
>
>
> The two solutions seems not very smart and they are very running
> slowly. I wonder if there is any library function that can compare two
> file contents just like "strcmp" to compare two string.
> Or maybe there are some other smart ways to achieve it.
>
> Thank you for suggestions.


I'm pretty sure there isn't one.

<OT>
A few things spring to mind, though.

A) if you are going to use a shell cmd, try 'cmp' instead of diff. It
handles binary files, too *and* it's
faster.

B) if speed is important, try to improve it by doing a 'stat()' and compare
files-size. If the size is
different, the two files cannot be the same.

C) if you *must* compare actual files, use fread to read entire blocks and
do the comparison in
memory. Allocate relative large blocks and end on the first actual
difference.

</OT>


 
Reply With Quote
 
 
 
 
Charlie Gordon
Guest
Posts: n/a
 
      11-24-2004
"dawn" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> Hi all,
> I am now working on a C program under Unix. [...]


There is no support for enumerating directory contents in standard C, you should
seek help on a unix newsgroup or pull the man pages for opendir(), readdir(),
closedir() and related APIs.

--
Chqrlie.


 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      11-24-2004
In <(E-Mail Removed)> dawn <(E-Mail Removed)> writes:

> I am now working on a C program under Unix.


And you're too stupid to realise that comp.unix.programmer is the right
place for your question.

> The requirement for the program is that:
> A file name is passed to program as a parameter. The program will
>Find files under a specified directory. The matched file must have the
>same content with the given file. It does not matter whether the
>filenames are the same.


Standard C provides no support for directories, which is why posting to
comp.lang.c was such a silly thing.

> It is easy to find file that has the same name with given file, but
>may be hard to find the files that with the same content. In my
>knowledge, i give two solutions:
>
> 1) Go throught the directory and its sub direcotry tree, and when
>meeting a file, Use the stand C library function to open that file and
>the given file, and then compare those contents in buffers to see if
>they are the same.


The Unix function ftw() does most of the job for you. You only have to
provide a function for comparing the contents of the two files and such
a function is damn fast if you do it right: compare the contents only
if the files have the same size, use fread() and memcmp() to perform the
comparison on BUFSIZ sized chunks. Note that ftw is giving you the stat
info about each directory entry, so you don't have to do anything special
in order to obtain the file size.

For extra points, lock *at least* the reference file, to be sure that its
contents is stable during the program execution. But don't ask here how!

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: http://www.velocityreviews.com/forums/(E-Mail Removed)
Currently looking for a job in the European Union
 
Reply With Quote
 
fallujahdestrotedbyusbastards@peace.com
Guest
Posts: n/a
 
      11-25-2004
On 24 Nov 2004 17:00:35 GMT, (E-Mail Removed) (Dan Pop) wrote:

>In <(E-Mail Removed)> dawn <(E-Mail Removed)> writes:
>
>> I am now working on a C program under Unix.

>
>And you're too stupid to realise that comp.unix.programmer is the right
>place for your question.
>

You unbelieveably ignorant arsehole. He is writing a C program
therefore this is exactly the right place to ask his question.
Are you a yank ?

 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      11-25-2004
On Thu, 25 Nov 2004 21:20:55 +0000, in comp.lang.c ,
(E-Mail Removed) wrote:

>On 24 Nov 2004 17:00:35 GMT, (E-Mail Removed) (Dan Pop) wrote:
>
>>In <(E-Mail Removed)> dawn <(E-Mail Removed)> writes:
>>
>>> I am now working on a C program under Unix.

>>
>>And you're too stupid to realise that comp.unix.programmer is the right
>>place for your question.
>>

>You unbelieveably ignorant arsehole. He is writing a C program
>therefore this is exactly the right place to ask his question.


Not if the question relates to unix features. C is not unix. Since between
you adn Dan you snipped the entire original posting, its not possible to
say now.

>Are you a yank ?


Dan is swiss or german, and one of the serious experts round here, if
lacking in much social grace.

But don't let that stop you ranting mindlessly.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
 
Reply With Quote
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      11-26-2004
(E-Mail Removed) wrote:
> On 24 Nov 2004 17:00:35 GMT, (E-Mail Removed) (Dan Pop) wrote:


>>In <(E-Mail Removed)> dawn <(E-Mail Removed)> writes:
>>
>>> I am now working on a C program under Unix.

>>
>>And you're too stupid to realise that comp.unix.programmer is the right
>>place for your question.
>>

> You unbelieveably ignorant arsehole.


And what are you just proving to be? Let's see...

> He is writing a C program
> therefore this is exactly the right place to ask his question.


No, he isn't right here. Would you do the absolute minimum of reading
a few posts in the group before jumping in you would know very well
that discussions about platform specific extensions to C is off-topic
here. So far about your claim about Dan's "ignorance". Looks a lot
more like it's the other way round. And Dan even went so far to
provide lots of useful information for the OP, which you didn't.

> Are you a yank ?


Well, that kind of thing obviously shows that rather you are the
'asshole' here. Or a dimwit. Pick what you prefer. Case closed.
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      11-26-2004
Mark McIntyre <(E-Mail Removed)> wrote:

> On Thu, 25 Nov 2004 21:20:55 +0000, in comp.lang.c ,
> (E-Mail Removed) wrote:
>
> >Are you a yank ?

>
> Dan is swiss or german,


Rumanian, surely?

Richard
 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      11-26-2004
Richard Bos wrote:

> Mark McIntyre <(E-Mail Removed)> wrote:
>
>
>>On Thu, 25 Nov 2004 21:20:55 +0000, in comp.lang.c ,
>>(E-Mail Removed) wrote:
>>
>>
>>>Are you a yank ?

>>
>>Dan is swiss or german,

>
> Rumanian, surely?


Awgh, are you guys regulars? Do I know you?
Apart from the uselessness of this information: Have a
look at threads with Dan sniping at Joona I Palaste...


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      11-26-2004
In <(E-Mail Removed)> (E-Mail Removed) (Richard Bos) writes:

>Mark McIntyre <(E-Mail Removed)> wrote:
>
>> On Thu, 25 Nov 2004 21:20:55 +0000, in comp.lang.c ,
>> (E-Mail Removed) wrote:
>>
>> >Are you a yank ?

>>
>> Dan is swiss or german,

>
>Rumanian, surely?


According to Mark's logic, I must be Swiss or German because I am known
to have a Swiss email address and a German email address. Now, where does
this Romanian nonsense come from?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
Currently looking for a job in the European Union
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding all the links in a Unix file/directory path freesoft12@gmail.com Perl Misc 3 05-12-2009 09:42 PM
Finding the number of UIDs on unix frikk C++ 2 04-12-2007 10:09 PM
compile C programs with UNIX system calls (= Unix Programs??) jrefactors@hotmail.com C Programming 18 01-10-2005 03:35 AM
compile C programs with UNIX system calls (= Unix Programs??) jrefactors@hotmail.com C++ 12 01-10-2005 03:35 AM
my own perl "dos->unix"/"unix->dos" Robert Wallace Perl Misc 7 01-22-2004 10:59 PM



Advertisments