Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 740493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:31:20+00:00 2026-05-14T08:31:20+00:00

I have written a function that checks if to files are duplicates or not.

  • 0

I have written a function that checks if to files are duplicates or not. This function signature is:

int check_dup_memmap(char *f1_name, char *f2_name)

It returns:

  • (-1) – If something went wrong;
  • (0) – If the two files are similar;
  • (+1) – If the two files are different;

The next step is to write a function that iterates through all the files in a certain directory,apply the previous function, and gives a report on every existing duplicates.

Initially I’ve thought to write a function that generates a file with all the filenames in a certain directory and then, read that file again and gain and compare every two files. Here is that version of the function, that gets all the filenames in a certain directory.

void *build_dir_tree(char *dirname, FILE *f)
{
    DIR *cdir = NULL;
    struct dirent *ent = NULL;
    struct stat buf;
    if(f == NULL){
        fprintf(stderr, "NULL file submitted. [build_dir_tree].\n");
        exit(-1);   
    }
    if(dirname == NULL){
        fprintf(stderr, "NULL dirname submitted. [build_dir_tree].\n");
        exit(-1);
    }
    if((cdir = opendir(dirname)) == NULL){
        char emsg[MFILE_LEN];
        sprintf(emsg, "Cannot open dir: %s [build_dir_tree]\t",dirname);
        perror(emsg);
    }
    chdir(dirname);
    while ((ent = readdir(cdir)) != NULL) {
        lstat(ent->d_name, &buf);
        if (S_ISDIR(buf.st_mode)) {
            if (strcmp(".", ent->d_name) == 0 ||
                    strcmp("..", ent->d_name) == 0) {
                continue;
            }
            build_dir_tree(ent->d_name, f);
        }
        else{
            fprintf(f, "/%s/%s\n",util_get_cwd(),ent->d_name);
        }
    }
    chdir("..");
    closedir(cdir);
}

Still I consider this approach a little inefficient, as I have to parse the file again and again.

In your opinion what are other approaches should I follow:

  • Write a datastructure and hold the files instead of writing them in the file ? I think for a directory with a lot of files, the memory will become very fragmented.
  • Hold all the filenames in auto-expanding array, so that I can easy access every file by their index, because they will in a contiguous memory location.
  • Map this file in memory using mmap() ? But mmap may fail, as the file gets to big.

Any opinions on this. I want to choose the most efficient path, and access as few resources as possible. This is the requirement of the program…

EDIT:
Is there a way to get the numbers of files in a certain directory, without iterating through it ?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:31:20+00:00Added an answer on May 14, 2026 at 8:31 am

    You probably want to use a hash or checksum; create a hash of the content of each file, and associate each hash with a list of filenames with that hash. Then, when you construct a hash, you will see what other files also share the same hash. Only if you get a collision will you actually have to compare the full files. If you choose a good hash function, then collisions should be relatively rare, making full comparisons rare.

    Note that there is a tradeoff between the size of the hash and the number of collisions; if you use a smaller hash, the collisions will probably be more frequent, but you will use less space per file, and if you use a larger hash, then you will have to do fewer full file comparisons, but will need to hold onto and compare larger hashes. Also, some hash functions result in fewer collisions than other hashes, but it is possible that some better hash functions may be more time consuming and compute intensive than poorer ones.

    An efficient method of file and directory traversal is to use ftw or nftw.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written following function which checks whether start_date field is not empty and
I have written a function that sorts a big scale of data. To test
I have written a function that extract the domain from hostname. e.g. www.domain.com ->
I have written a function that positions a tooltip just above a textbox. The
I have written a javascript function that uses setInterval to manipulate a string every
I have written code to perform a function that could take a while to
I have written a PHP function to take a video embed code that has
I currently have a function written called saveWorkout that saves an NSMutableArray to another
I have the view function in django that written like a dispatcher calling other
I have written this function to auto correct gender to M or F from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.