Unix C programming for finding file

Discussion in 'C Programming' started by dawn, Nov 24, 2004.

  1. dawn

    dawn Guest

    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, Nov 24, 2004
    #1
    1. Advertising

  2. dawn

    dandelion Guest

    [OT] Re: Unix C programming for finding file

    "dawn" <> wrote in message
    news:...
    > 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>
     
    dandelion, Nov 24, 2004
    #2
    1. Advertising

  3. "dawn" <> wrote in message news:...
    > 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.
     
    Charlie Gordon, Nov 24, 2004
    #3
  4. dawn

    Dan Pop Guest

    In <> dawn <> 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:
    Currently looking for a job in the European Union
     
    Dan Pop, Nov 24, 2004
    #4
  5. dawn

    Guest

    On 24 Nov 2004 17:00:35 GMT, (Dan Pop) wrote:

    >In <> dawn <> 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 ?
     
    , Nov 25, 2004
    #5
  6. On Thu, 25 Nov 2004 21:20:55 +0000, in comp.lang.c ,
    wrote:

    >On 24 Nov 2004 17:00:35 GMT, (Dan Pop) wrote:
    >
    >>In <> dawn <> 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>
     
    Mark McIntyre, Nov 25, 2004
    #6
  7. dawn

    -berlin.de Guest

    wrote:
    > On 24 Nov 2004 17:00:35 GMT, (Dan Pop) wrote:


    >>In <> dawn <> 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 ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Nov 26, 2004
    #7
  8. dawn

    Richard Bos Guest

    Mark McIntyre <> wrote:

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

    >
    > Dan is swiss or german,


    Rumanian, surely?

    Richard
     
    Richard Bos, Nov 26, 2004
    #8
  9. dawn

    Michael Mair Guest

    Richard Bos wrote:

    > Mark McIntyre <> wrote:
    >
    >
    >>On Thu, 25 Nov 2004 21:20:55 +0000, in comp.lang.c ,
    >> 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.
     
    Michael Mair, Nov 26, 2004
    #9
  10. dawn

    Dan Pop Guest

    In <> (Richard Bos) writes:

    >Mark McIntyre <> wrote:
    >
    >> On Thu, 25 Nov 2004 21:20:55 +0000, in comp.lang.c ,
    >> 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:
    Currently looking for a job in the European Union
     
    Dan Pop, Nov 26, 2004
    #10
  11. dawn

    Joe Wright Guest

    Richard Bos wrote:
    > Mark McIntyre <> wrote:
    >
    >
    >>On Thu, 25 Nov 2004 21:20:55 +0000, in comp.lang.c ,
    >> wrote:
    >>
    >>
    >>>Are you a yank ?

    >>
    >>Dan is swiss or german,

    >
    >
    > 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?
    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Nov 26, 2004
    #11
  12. dawn

    Dan Pop Guest

    In <> Joe Wright <> writes:

    >Richard Bos wrote:
    >>
    >> Rumanian, surely?
    >>
    >> Richard

    >
    >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
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Currently looking for a job in the European Union
     
    Dan Pop, Nov 26, 2004
    #12
  13. In article <co7q5e$deo$>, Dan Pop <> wrote:

    >"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
     
    Richard Tobin, Nov 26, 2004
    #13
  14. "Richard Tobin" <> wrote in message
    news:co87e1$1mvn$...
    > 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.
     
    Craig Barkhouse, Nov 26, 2004
    #14
  15. In article <p0Opd.60663$>,
    Craig Barkhouse <> wrote:

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


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

    -- Richard
     
    Richard Tobin, Nov 26, 2004
    #15
  16. On Fri, 26 Nov 2004 07:41:50 GMT, in comp.lang.c ,
    (Richard Bos) wrote:

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

    >>
    >> Dan is swiss or german,

    >
    >Rumanian, surely?


    same difference.

    gd&rvf
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
     
    Mark McIntyre, Nov 26, 2004
    #16
  17. dawn

    Joe Wright Guest

    Richard Tobin wrote:
    > In article <p0Opd.60663$>,
    > Craig Barkhouse <> wrote:
    >
    >
    >>>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.

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


    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?

    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Nov 27, 2004
    #17
  18. On Fri, 26 Nov 2004 21:21:10 -0500, in comp.lang.c , Joe Wright
    <> wrote:

    >Richard Tobin wrote:
    >> In article <p0Opd.60663$>,
    >> Craig Barkhouse <> wrote:
    >>
    >>>>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.

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

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

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
     
    Mark McIntyre, Nov 27, 2004
    #18
  19. Dan Pop <> scribbled the following:
    > In <> Joe Wright <> writes:
    >>How does Dan spell it?


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


    Interesting, how is that pronounced?

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "Normal is what everyone else is, and you're not."
    - Dr. Tolian Soran
     
    Joona I Palaste, Nov 27, 2004
    #19
  20. dawn

    Joe Wright Guest

    Joona I Palaste wrote:
    > Dan Pop <> scribbled the following:
    >
    >>In <> Joe Wright <> writes:
    >>
    >>>How does Dan spell it?

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

    >
    >
    > 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?
    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Nov 28, 2004
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    12
    Views:
    1,705
    Dave Thompson
    Jan 10, 2005
  2. Replies:
    18
    Views:
    663
    Dave Thompson
    Jan 10, 2005
  3. frikk
    Replies:
    2
    Views:
    393
    Default User
    Apr 12, 2007
  4. Robert Wallace

    my own perl "dos->unix"/"unix->dos"

    Robert Wallace, Jan 21, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    316
    Michele Dondi
    Jan 22, 2004
  5. Replies:
    3
    Views:
    184
    Jürgen Exner
    May 12, 2009
Loading...

Share This Page