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

  • Home
  • SEARCH
  • 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 8640663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:20:05+00:00 2026-06-12T11:20:05+00:00

How to check if one file is the same (has the same content) as

  • 0

How to check if one file is the same (has the same content) as the other file using Unix C? I mean, when I cant use fopen, fread, fclose but just open, read, close? I’m interested in answers which shows how to do this ONLY in Unix C.

I wrote a program that copies one file to another but have no idea how to check if they’re the same :/ :

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    const char *in_filename = "in.txt", *out_filename = "out.txt";
    int in_fd, out_fd, bytes_read, bytes_written;
    int buffsize = 512;
    char buffer[512];
    int success = 0;

    in_fd = open(in_filename, O_RDONLY);
    if (in_fd == -1)
        return -1;
    out_fd = open(out_filename, O_WRONLY | O_APPEND, S_IRUSR | S_IWUSR);
    if (out_fd == -1)
        return -1;

    for(;;)
    {
        bytes_read = read(in_fd, buffer, buffsize);
        if (bytes_read > 0)
        {
            bytes_written = write(out_fd, buffer, bytes_read);
            if(bytes_written < 0)
                return -1;
        }
        else
        {
            if (bytes_read == 0)
            {
                if (close(in_fd) < 0)
                    return -1;
                if (close(out_fd) < 0)
                    return -1;
                success = 1;
                break;
            }
            else if (bytes_read == -1)
            {
                break;
                return -1;
            }
        }
    }

    if(success)
        fprintf(stdout, "%s", "Success!\n");

    return 0;
}

Heres what I tried:

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    const char *in_filename = "in.txt", *out_filename = "out.txt";
    int in_fd, out_fd, bytes_read_in, bytes_read_out;
    int buffsize = 512;
    char in_buffer[512], out_buffer[512];
    int the_same = 0;

    in_fd = open(in_filename, O_RDONLY);
    if (in_fd == -1)
        return -1;
    out_fd = open(out_filename, O_RDONLY);
    if (out_fd == -1)
        return -1;

    for(;;)
    {
        bytes_read_in = read(in_fd, in_buffer, buffsize);
        if (bytes_read_in > 0)
        {
            bytes_read_out = read(out_fd, out_buffer, buffsize);
            if(bytes_read_out > 0)
            {
                int i = 0;
                for(i=0; i<buffsize; i++)
                {
                    if(in_buffer[i] != out_buffer[i])
                        the_same = 0;
                }
                the_same = 1;
            }
        }
        else
        {
            if (bytes_read_in == 0)
            {
                if (close(in_fd) < 0)
                    return -1;
                if (close(out_fd) < 0)
                    return -1;
                break;
            }
            else if (bytes_read_in == -1)
            {
                break;
                return -1;
            }
        }
    }

    if(the_same)
        fprintf(stdout, "%s", "Files are the same!\n");

    return 0;
}

but it shows that files are the same, while theyre not 🙁

  • 1 1 Answer
  • 0 Views
  • 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-06-12T11:20:06+00:00Added an answer on June 12, 2026 at 11:20 am

    You just have to read two buffers in the same time. For instance (also, think to handle errors), without using C standard library at all:

    #include <fcntl.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <unistd.h>
    
    #define BUFFER_SIZE 1024
    
    static int 
    bufcmp(const void *p, const void *q, size_t n)
    {
        const char *p1 = p;
        const char *p2 = q;
    
        while (n-- > 0) {
            if (*p1++ != *p2++)
                return 0;
        }
    
        return 1;
    }
    
    int
    main(int argc, char *argv[]) 
    {
        int fd1 = open(argv[1], O_RDONLY);
        int fd2 = open(argv[2], O_RDONLY);
        int same = 1;
    
        for (;;) {
            char buf1[BUFFER_SIZE], buf2[BUFFER_SIZE];
            ssize_t n1 = read(fd1, buf1, BUFFER_SIZE);
            ssize_t n2 = read(fd2, buf2, BUFFER_SIZE);
    
            if (n1 < n2) {
                same = 0;
                break;
            } else if (n1 == 0) {
                break;
            } else if (bufcmp(buf1, buf2, n1) == 0) {
                same = 0;
                break;
            }
        }
    
        if (same)
            write(STDOUT_FILENO, "Same content.\n", 14);
    
        close(fd1);
        close(fd2);    
    
        return 0;
    }
    

    NB (Thanks to user4815162342): This code is not wholly right. Indeed, it is not an error if the number of bytes read returned by read is smaller than the number of bytes requested. However, to shorten this code, I didn’t include this management.

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

Sidebar

Related Questions

I'm working in C#/.NET and I'm parsing a file to check if one line
Based on the following question: Check if one string is a rotation of other
When using custom assemblies in a visual studio project. How does one check in
My goal is to install only one WAR file but to use test or
I have a checkboxlist.When I check one the value will appear in a table.Now
I have this control (see picture). I like when check one option in this
I know how to check if one textbox is changed, but what about if
Basically I just want to check if one time period overlaps with another. Null
During reflection, is it possible in C# to check whether one constructor calls another?
I apologize if it is stupid question. I wanted to check whether one or

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.