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 6069359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:48:52+00:00 2026-05-23T09:48:52+00:00

I’m writing a program that compares two files character by character. The function to

  • 0

I’m writing a program that compares two files character by character. The function to compare each file returns a value dependant on the condition of the files.

the function returns 0 when both files are the same, -1 if both files match but the first file ends before the second, -2 if both files match but the second file ends before the first, and a positive int indicating which character the files differ at.

#include <stdio.h>
#include <string.h>
#define CMP_EQUAL 0
#define CMP_EOF_FIRST -1
#define CMP_EOF_SECOND -2

int char_cmp(FILE *fp1, FILE *fp2);

int main(void)
{
    FILE *fp1;
    FILE *fp2;

    fp1 = fopen("input1.txt", "rb+");
    fp2 = fopen("input2.txt", "rb+");

    switch(char_cmp(fp1, fp2))
    {
        case CMP_EQUAL:
            printf("The Files are equal");
            break;
        case CMP_EOF_FIRST:
            printf("EOF on a.txt");
            break;
        case CMP_EOF_SECOND:
            printf("EOF on t.txt");
            break;
        default:
            printf("files differ: char %d\n", char_cmp(fp1, fp2));
            break;
    }

    if(fclose(fp1) != 0)
    {
        perror("fclose");
        /*other error handling*/
    }

    if(fclose(fp2) != 0)
    {
        perror("fclose");
        /*other error handling*/
    }

    return 0;
}

int char_cmp(FILE *fp1, FILE *fp2)
{
    int c, d;
    size_t byte = 0;
    int same = 1;

    do
    {
        byte++;
    }while((c = fgetc(fp1)) == (d = fgetc(fp2)));

    if(c == EOF && d != EOF)
    {
        return CMP_EOF_FIRST;
    }

    if(d == EOF && c != EOF)
    {
        return CMP_EOF_SECOND;
    }

    if(c != d)
    {
        return byte;
    }

    return CMP_EQUAL;
}

I was wondering how i would break out of the do loop after checking if all the characters match in each file. Because when i have tried, It breaks the moment it finds a character that is the same and does not check the rest of it.

Also i’ve encourtered this weird bug where if one file contains:

dee

and the second one contains

ae

it gives me a weird return value and was wondering why is that so?

thanks for any help in advance

  • 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-05-23T09:48:52+00:00Added an answer on May 23, 2026 at 9:48 am

    You call char_cmp(fp1, fp2)) twice – once in the switch statement, and the second time in the default condition. The second time it returns you the second char position in which they differ (or something another, really unexpected 🙂
    Change it to

    int k = char_cmp(fp1, fp2));
    

    and use k in these both places:

    switch( k )
    ...
    printf("files differ: char %d\n", k);
    

    EDIT: The infinite loop in case of equal files happens because in this condition:

    (c = fgetc(fp1)) == (d = fgetc(fp2))
    

    c and d become forever equal to EOF from some moment. Change it to

    (c = fgetc(fp1)) == (d = fgetc(fp2) && c != EOF
    

    and everything is ok.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I have a bunch of posts stored in text files formatted in yaml/textile (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.