Unix C programming for finding file

D

dawn

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.
 
D

dandelion

dawn said:
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>
 
C

Charlie Gordon

dawn said:
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.
 
D

Dan Pop

In said:
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
 
F

fallujahdestrotedbyusbastards

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 ?
 
M

Mark McIntyre

On Thu, 25 Nov 2004 21:20:55 +0000, in comp.lang.c ,
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.
 
J

Jens.Toerring

And you're too stupid to realise that comp.unix.programmer is the right
place for your question.
You unbelieveably ignorant arsehole.[/QUOTE]

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.
 
M

Michael Mair

Richard said:
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
 
D

Dan Pop

In said:
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
 
J

Joe Wright

Richard said:
Rumanian, surely?

Richard

Romanian.

Today's French and English texts refer to Romania while older German
seems to like Rumania. What's the difference?

How does Dan spell it?
 
D

Dan Pop

In said:
Romanian.

Today's French and English texts refer to Romania while older German
seems to like Rumania. What's the difference?

"Rumania" and "Roumania" are a obsolete English spellings. "Romania"
is the current official English spelling. The French use "Roumanie"
and the Germans "Rumaenien" ("Rumänien" for people using Latin-1
fonts). The international abbreviation is RO, BTW.
How does Dan spell it?

When using a Latin-1 or Latin-2 font and writing in my native language:
"România".

Dan
 
R

Richard Tobin

"Rumania" and "Roumania" are a obsolete English spellings. "Romania"
is the current official English spelling.

I wasn't aware that we had official spellings in English.

Do you mean it's the one that the Romanian government wants us to use?

-- Richard
 
C

Craig Barkhouse

Richard Tobin said:
I wasn't aware that we had official spellings in English.

We do. In fact, there have been entire books -- called "dictionaries" -- on
the subject.
 
R

Richard Tobin

I wasn't aware that we had official spellings in English.
[/QUOTE]
We do. In fact, there have been entire books -- called "dictionaries" -- on
the subject.

How strange. I was not aware that they had an "official" status at all.

-- Richard
 
J

Joe Wright

Richard said:
We do. In fact, there have been entire books -- called "dictionaries" -- on
the subject.


How strange. I was not aware that they had an "official" status at all.

-- Richard[/QUOTE]

It's the Queen's English isn't it? How 'official' do you want it?
But seriously, it doesn't matter how the British, the French, the
Russians or anybody else spells it. How is it spelled in Bucharest?
 
M

Mark McIntyre

It's the Queen's English isn't it? How 'official' do you want it?

Exactly. There's no "legal" spelling of most words, but there is an
official one - for english English, the OED's spelling is as official as it
gets, and its the one you;ll get taught in school. US English probably has
several different competing standards, as seems to be commonplace in other
walks of life there.
 
J

Joe Wright

Joona said:
Interesting, how is that pronounced?

Like its spelled. The circumflex over the â flattens the a sound
some. We (English speaking) might be otherwise say ro.MAIN.ya rather
than the desired ro.MAHN.ya. That's just a quess of course.

Dan?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top