Unix C programming for finding file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dawn

    #1

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

    #2
    [OT] Re: Unix C programming for finding file


    "dawn" <ming@lian.co m> wrote in message
    news:30jp61F31b de9U1@uni-berlin.de...[color=blue]
    > 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.[/color]

    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>


    Comment

    • Charlie Gordon

      #3
      Re: Unix C programming for finding file

      "dawn" <ming@lian.co m> wrote in message news:30jp61F31b de9U1@uni-berlin.de...[color=blue]
      > Hi all,
      > I am now working on a C program under Unix. [...][/color]

      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.


      Comment

      • Dan Pop

        #4
        Re: Unix C programming for finding file

        In <30jp61F31bde9U 1@uni-berlin.de> dawn <ming@lian.co m> writes:
        [color=blue]
        > I am now working on a C program under Unix.[/color]

        And you're too stupid to realise that comp.unix.progr ammer is the right
        place for your question.
        [color=blue]
        > 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.[/color]

        Standard C provides no support for directories, which is why posting to
        comp.lang.c was such a silly thing.
        [color=blue]
        > 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.[/color]

        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: Dan.Pop@ifh.de
        Currently looking for a job in the European Union

        Comment

        • fallujahdestrotedbyusbastards@peace.com

          #5
          Re: Unix C programming for finding file

          On 24 Nov 2004 17:00:35 GMT, Dan.Pop@cern.ch (Dan Pop) wrote:
          [color=blue]
          >In <30jp61F31bde9U 1@uni-berlin.de> dawn <ming@lian.co m> writes:
          >[color=green]
          >> I am now working on a C program under Unix.[/color]
          >
          >And you're too stupid to realise that comp.unix.progr ammer is the right
          >place for your question.
          >[/color]
          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 ?

          Comment

          • Mark McIntyre

            #6
            Re: Unix C programming for finding file

            On Thu, 25 Nov 2004 21:20:55 +0000, in comp.lang.c ,
            fallujahdestrot edbyusbastards@ peace.com wrote:
            [color=blue]
            >On 24 Nov 2004 17:00:35 GMT, Dan.Pop@cern.ch (Dan Pop) wrote:
            >[color=green]
            >>In <30jp61F31bde9U 1@uni-berlin.de> dawn <ming@lian.co m> writes:
            >>[color=darkred]
            >>> I am now working on a C program under Unix.[/color]
            >>
            >>And you're too stupid to realise that comp.unix.progr ammer is the right
            >>place for your question.
            >>[/color]
            >You unbelieveably ignorant arsehole. He is writing a C program
            >therefore this is exactly the right place to ask his question.[/color]

            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.
            [color=blue]
            >Are you a yank ?[/color]

            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 >

            Comment

            • Jens.Toerring@physik.fu-berlin.de

              #7
              Re: Unix C programming for finding file

              fallujahdestrot edbyusbastards@ peace.com wrote:[color=blue]
              > On 24 Nov 2004 17:00:35 GMT, Dan.Pop@cern.ch (Dan Pop) wrote:[/color]
              [color=blue][color=green]
              >>In <30jp61F31bde9U 1@uni-berlin.de> dawn <ming@lian.co m> writes:
              >>[color=darkred]
              >>> I am now working on a C program under Unix.[/color]
              >>
              >>And you're too stupid to realise that comp.unix.progr ammer is the right
              >>place for your question.
              >>[/color]
              > You unbelieveably ignorant arsehole.[/color]

              And what are you just proving to be? Let's see...
              [color=blue]
              > He is writing a C program
              > therefore this is exactly the right place to ask his question.[/color]

              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.
              [color=blue]
              > Are you a yank ?[/color]

              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 ___ Jens.Toerring@p hysik.fu-berlin.de
              \______________ ____________ http://www.toerring.de

              Comment

              • Richard Bos

                #8
                Re: Unix C programming for finding file

                Mark McIntyre <markmcintyre@s pamcop.net> wrote:
                [color=blue]
                > On Thu, 25 Nov 2004 21:20:55 +0000, in comp.lang.c ,
                > fallujahdestrot edbyusbastards@ peace.com wrote:
                >[color=green]
                > >Are you a yank ?[/color]
                >
                > Dan is swiss or german,[/color]

                Rumanian, surely?

                Richard

                Comment

                • Michael Mair

                  #9
                  Re: Unix C programming for finding file

                  Richard Bos wrote:
                  [color=blue]
                  > Mark McIntyre <markmcintyre@s pamcop.net> wrote:
                  >
                  >[color=green]
                  >>On Thu, 25 Nov 2004 21:20:55 +0000, in comp.lang.c ,
                  >>fallujahdestr otedbyusbastard s@peace.com wrote:
                  >>
                  >>[color=darkred]
                  >>>Are you a yank ?[/color]
                  >>
                  >>Dan is swiss or german,[/color]
                  >
                  > Rumanian, surely?[/color]

                  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.

                  Comment

                  • Dan Pop

                    #10
                    Re: Unix C programming for finding file

                    In <41a6de28.15177 5482@news.indiv idual.net> rlb@hoekstra-uitgeverij.nl (Richard Bos) writes:
                    [color=blue]
                    >Mark McIntyre <markmcintyre@s pamcop.net> wrote:
                    >[color=green]
                    >> On Thu, 25 Nov 2004 21:20:55 +0000, in comp.lang.c ,
                    >> fallujahdestrot edbyusbastards@ peace.com wrote:
                    >>[color=darkred]
                    >> >Are you a yank ?[/color]
                    >>
                    >> Dan is swiss or german,[/color]
                    >
                    >Rumanian, surely?[/color]

                    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: Dan.Pop@ifh.de
                    Currently looking for a job in the European Union

                    Comment

                    • Joe Wright

                      #11
                      Re: Unix C programming for finding file

                      Richard Bos wrote:[color=blue]
                      > Mark McIntyre <markmcintyre@s pamcop.net> wrote:
                      >
                      >[color=green]
                      >>On Thu, 25 Nov 2004 21:20:55 +0000, in comp.lang.c ,
                      >>fallujahdestr otedbyusbastard s@peace.com wrote:
                      >>
                      >>[color=darkred]
                      >>>Are you a yank ?[/color]
                      >>
                      >>Dan is swiss or german,[/color]
                      >
                      >
                      > Rumanian, surely?
                      >
                      > Richard[/color]

                      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:joewwrig ht@comcast.net
                      "Everything should be made as simple as possible, but not simpler."
                      --- Albert Einstein ---

                      Comment

                      • Dan Pop

                        #12
                        Re: Unix C programming for finding file

                        In <S8udnR9m1JORwT rcRVn-2Q@comcast.com> Joe Wright <joewwright@com cast.net> writes:
                        [color=blue]
                        >Richard Bos wrote:[color=green]
                        >>
                        >> Rumanian, surely?
                        >>
                        >> Richard[/color]
                        >
                        >Romanian.
                        >
                        >Today's French and English texts refer to Romania while older German
                        >seems to like Rumania. What's the difference?[/color]

                        "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.
                        [color=blue]
                        >How does Dan spell it?[/color]

                        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: Dan.Pop@ifh.de
                        Currently looking for a job in the European Union

                        Comment

                        • Richard Tobin

                          #13
                          Re: Unix C programming for finding file

                          In article <co7q5e$deo$2@s unnews.cern.ch> , Dan Pop <Dan.Pop@cern.c h> wrote:
                          [color=blue]
                          >"Rumania" and "Roumania" are a obsolete English spellings. "Romania"
                          >is the current official English spelling.[/color]

                          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

                          Comment

                          • Craig Barkhouse

                            #14
                            Re: Unix C programming for finding file


                            "Richard Tobin" <richard@cogsci .ed.ac.uk> wrote in message
                            news:co87e1$1mv n$2@pc-news.cogsci.ed. ac.uk...[color=blue]
                            > I wasn't aware that we had official spellings in English.[/color]

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


                            Comment

                            • Richard Tobin

                              #15
                              Re: Unix C programming for finding file

                              In article <p0Opd.60663$Ro .2298486@news20 .bellglobal.com >,
                              Craig Barkhouse <cabarkho@stude nt.cs.uwaterloo .ca> wrote:
                              [color=blue][color=green]
                              >> I wasn't aware that we had official spellings in English.[/color][/color]
                              [color=blue]
                              >We do. In fact, there have been entire books -- called "dictionari es" -- on
                              >the subject.[/color]

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

                              -- Richard

                              Comment

                              Working...