用于查找文件的Unix C编程 [英] Unix C programming for finding file

查看:64
本文介绍了用于查找文件的Unix C编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我现在正在开发一个Unix下的C程序。

该程序的要求是:

A文件名作为参数传递给程序。该程序将

查找指定目录下的文件。匹配的文件必须与给定文件具有相同的内容。

文件名是否相同无关紧要。


很容易找到与给定文件具有相同名称的文件,但

可能很难找到具有相同内容的文件。在我的

知识中,我给出了两个解决方案:


1)浏览目录及其子目录树,当

遇到一个文件,使用stand C库函数打开该文件并给出给定文件
,然后比较缓冲区中的这些内容,看看它们是否相同




2)浏览目录及其子deirectroy树,当

遇到一个文件时,执行system shell命令diff。比较

两个文件,看它们的内容是否相同。

两个解决方案看起来不是很聪明,而且它们非常运行
$ b $慢慢地。我想知道是否有任何库函数可以比较两个

文件内容,就像strcmp一样。比较两个字符串。

或者可能有其他一些聪明的方法来实现它。


感谢您的建议。

解决方案



" dawn" < MI ** @ lian.com>在消息中写道

news:30 ************* @ uni-berlin.de ...

大家好,
我现在正在使用Unix下的C程序。
程序的要求是:
文件名作为参数传递给程序。该程序将在指定目录下查找文件。匹配的文件必须与给定文件具有相同的内容。
文件名是否相同并不重要。

很容易找到与给定文件具有相同名称的文件,但
可能很难找到文件具有相同内容的。在我的知识中,我给出了两个解决方案:

1)浏览目录及其子目录树,当
遇到文件时,使用stand C库函数打开该文件和给定文件,然后比较缓冲区中的这些内容,看看它们是否相同。

2)浏览目录及其子deirectroy树,当
遇到一个文件时,执行system shell命令diff。比较
两个文件,看它们内容是否相同。

这两个解决方案看起来不是很聪明,而且它们运行起来很慢。我想知道是否有任何库函数可以比较两个
文件内容,就像strcmp一样。比较两个字符串。
或许还有其他一些聪明的方法来实现它。

感谢您的建议。




我很确定没有一个。


< OT>

但有些事情会浮现在脑海中。


A)如果您要使用shell cmd,请尝试''cmp''而不是diff。它

也处理二进制文件*和*它'

更快。


B)如果速度很重要,尝试通过执行''stat()''来改进它并比较

文件大小。如果大小不同,那么两个文件就不一样了。


C)如果你*必须*比较实际文件,使用fread来读取整个块和



内存中进行比较。分配相对较大的块并以第一个实际的

差异结束。


< / OT>


< BLOCKQUOTE>"黎明" < MI ** @ lian.com>在消息新闻中写道:30 ************* @ uni-berlin.de ...

大家好,
我现在正在研究C Unix下的程序。 [...]




不支持在标准C中枚举目录内容,你应该在unix新闻组上寻求帮助或者b $ b拉动opendir(),readdir(),

closedir()和相关API的手册页。


-

Chqrlie。


In< 30 ************* @ uni-berlin.de>黎明< mi ** @ lian.com>写道:

我现在正在开发一个Unix下的C程序。


你觉得comp.unix.programmer对你的问题来说是个合适的

的地方你太傻了。

该程序的要求是:
文件名作为参数传递给程序。该程序将在指定目录下查找文件。匹配的文件必须与给定文件具有相同的内容。
文件名是否相同无关紧要。


标准C不提供对目录的支持,这就是为什么发布到

comp.lang.c是如此愚蠢的事情。

很容易找到与给定文件具有相同名称的文件,但
可能很难找到具有相同内容的文件。在我的知识中,我给出了两个解决方案:

1)浏览目录及其子目录树,当
遇到文件时,使用stand C库函数打开该文件和给定文件,然后比较缓冲区中的这些内容,看看它们是否相同。




Unix函数ftw()完成了大部分工作。你只需要
提供一个比较两个文件内容的功能,如果你这样做,那么
a函数就快了:只比较内容

如果文件大小相同,请使用fread()和memcmp()在BUFSIZ大小的块上执行

比较。请注意,ftw为您提供有关每个目录条目的stat

信息,因此您无需为了获取文件大小而执行任何特殊的操作

。 br />

对于额外的积分,请至少锁定*参考文件,以确保在程序执行期间其

内容是稳定的。但是不要问这里怎么样!


Dan

-

Dan Pop

DESY Zeuthen,RZ集团

电子邮件: Da*****@ifh.de

目前正在寻找欧盟的工作


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.

解决方案


"dawn" <mi**@lian.com> wrote in message
news:30*************@uni-berlin.de...

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>


"dawn" <mi**@lian.com> wrote in message news:30*************@uni-berlin.de...

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.


In <30*************@uni-berlin.de> dawn <mi**@lian.com> 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: Da*****@ifh.de
Currently looking for a job in the European Union


这篇关于用于查找文件的Unix C编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆