473,765 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unix C programming for finding file

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.
Nov 14 '05 #1
46 2536

"dawn" <mi**@lian.co m> 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>
Nov 14 '05 #2
"dawn" <mi**@lian.co m> 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.
Nov 14 '05 #3
In <30************ *@uni-berlin.de> dawn <mi**@lian.co m> writes:
I am now working on a C program under Unix.
And you're too stupid to realise that comp.unix.progr ammer 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
Nov 14 '05 #4
On 24 Nov 2004 17:00:35 GMT, Da*****@cern.ch (Dan Pop) wrote:
In <30************ *@uni-berlin.de> dawn <mi**@lian.co m> writes:
I am now working on a C program under Unix.


And you're too stupid to realise that comp.unix.progr ammer 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 ?

Nov 14 '05 #5
On Thu, 25 Nov 2004 21:20:55 +0000, in comp.lang.c ,
fa************* **************@ peace.com wrote:
On 24 Nov 2004 17:00:35 GMT, Da*****@cern.ch (Dan Pop) wrote:
In <30************ *@uni-berlin.de> dawn <mi**@lian.co m> writes:
I am now working on a C program under Unix.
And you're too stupid to realise that comp.unix.progr ammer 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 >
Nov 14 '05 #6
fa************* **************@ peace.com wrote:
On 24 Nov 2004 17:00:35 GMT, Da*****@cern.ch (Dan Pop) wrote:
In <30************ *@uni-berlin.de> dawn <mi**@lian.co m> writes:
I am now working on a C program under Unix.


And you're too stupid to realise that comp.unix.progr ammer 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 ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #7
Mark McIntyre <ma**********@s pamcop.net> wrote:
On Thu, 25 Nov 2004 21:20:55 +0000, in comp.lang.c ,
fa************* **************@ peace.com wrote:
Are you a yank ?


Dan is swiss or german,


Rumanian, surely?

Richard
Nov 14 '05 #8
Richard Bos wrote:
Mark McIntyre <ma**********@s pamcop.net> wrote:

On Thu, 25 Nov 2004 21:20:55 +0000, in comp.lang.c ,
fa*********** *************** *@peace.com 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.
Nov 14 '05 #9
In <41************ ****@news.indiv idual.net> rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Mark McIntyre <ma**********@s pamcop.net> wrote:
On Thu, 25 Nov 2004 21:20:55 +0000, in comp.lang.c ,
fa************* **************@ peace.com 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: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

270
3990
by: Jatinder | last post by:
I found these questions on a web site and wish to share with all of u out there,Can SomeOne Solve these Porgramming puzzles. Programming Puzzles Some companies certainly ask for these things. Specially Microsoft. Here are my favorite puzzles. Don't send me emails asking for the solutions.
5
2726
by: jrefactors | last post by:
when people say unix programmer, does it mean they write programs in unix environment,and those programs are run in unix platform? it is not necessary they are using unix function calls? I heard most of the time unix programmers are C and C++ programmers. please advise. thanks!!
48
2596
by: Daniel Rudy | last post by:
Hello, On a x86 machine, what is the format of a pointer in C? I know for a fact that the x86 p-mode uses a /selector:offset/ notation where the selector is defined in either the GDT or LDT. Does that carry over into the pointer, or does Unix use the flat memory model? -- Daniel Rudy
6
16390
by: Joseph | last post by:
Hi, I am trying to develop a C# application that will run on Windows that will do the following * Select file name to process * FTP the file to a UNIX server * Process this file on UNIX using a program on UNIX "./pgmname -options Infilename > OutputFilename" * FTP the output of this program back to Windows * Process the resultant file and create output report file
4
2371
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of which I mean here. Just for a moment, let's just take one definition for one of the
7
2068
by: programming | last post by:
Hi all, i have been having trouble with a login script that works on my windows machine, however when i upload it to the Unix server through VPN, the same script won't work! It won't parse member.txt properly i think. The password and usernames i am using are at the bottom of this post. Each time i go to login on the unix server, it clears the username and password field. I have been attempting to solve the problem, but have been...
2
1594
by: frikk | last post by:
Hey everyone, (Sorry about the first message if it made it - i got cut off early). I have a homework problem for my Operating Systems class. I am supposed to use a command such as "ps -aux" and return "N number of users, M number of processes". How am I to go about doing this? I am not very familiar with string parsing in c++, is this the only way? I am talking about getting the output from "ps -aux" and parsing it to retrieve the...
21
3038
by: Tom Gur | last post by:
Hi, It's seems that csh and tcsh acts a bit different when handling special characters in quotes. i.e: if i'll supply my program with the following arguments: -winpath "c:\\temp\\" tcsh will take it as -winpath "c:\temp\" and csh will take it literally (with the double-slashes). Is there a way for me to know what shell is currently running my
65
5093
by: Hongyu | last post by:
Dear all: I am trying to write to a file with full directory name and file name specified (./outdir/mytestout.txt where . is the current directory) in C programming language and under Unix, but got errors of Failed to open file ./outdir/mytestout.txt. Below is the code: #include <stdio.h>
0
9398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9951
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7378
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.