Results 1 to 7 of 7

Thread: C program to Compare two files

  1. #1
    Join Date
    Dec 2009
    Posts
    38

    C program to Compare two files

    Hello friends,

    I an studying the C programming language. The syllabus is near to complete. But then also have one query to ask you. I am trying to code the c program to compare two files. I don't know which logic I need to implement in my c program code to compare two files. If you posses any c code for comparing two files then please let me know that. your help would be appreciated.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: C program to Compare two files

    Hi, Try below C program to Compare two files:
    #include<stdlib.h>
    #include<string.h>
    #include<stdio.h>

    int cmpfles(void);
    void main()

    {
    int rt =cmpfles();
    printf("tn %d",result);
    }

    int cmpfles(void)
    {
    FILE *fpn1.*fpn2;

    fpn1 = fopen(file1,'rn');
    fpn2 = fopen(file1,'rn');

    char c1n[15],cn2[15];
    fgets(cn1,16,fpn1);
    fgets(cn2,16,fpn2);

    if(strcmp(cn1,cn2) = = 0)
    {
    return 1;
    }

    else return 0;
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: C program to Compare two files

    Hi friend,

    Before code program for comparing two files first you need to write code to open these two files . To open files in C you need to use the function known as 'fopen(fileName,'r')'. After coding this then you need to write code to compare files. And for this use the 'strcmp(FileName1,FileName2)' function. You need to mention the name of files which you wan to compare. Use this two function and let me know if are getting any errors.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: C program to Compare two files

    Try something below code to compare two files:
    main()
    {

    CFiles();
    return 0;
    }

    CFiles()
    {
    FILE * cfp1;
    FILE * cfp2;
    char cx1[100] cx2[100];
    int cmpx;

    cfp1 fopen("Doc1.txt" "r");
    cfp2 fopen("Doc2.txt" "r");

    if((cfp1 NULL) || (cfp2 NULL))
    {
    printf("Error occurs in the file n");
    }
    else
    {
    while(fgets(cx1 15 cfp1) ! NULL)
    puts(cx1);

    while(fgets(cx2 15 cfp2) ! NULL)
    puts(cx2);

    if((cmpx strcmp(cx1 cx2)) 0)
    {
    printf("Files are same n");
    }

    else
    {
    printf("Files are differnt n");
    }
    }
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: C program to Compare two files

    I think you should use the following while loop code in your C program for compare to files. In this code I have implemented the programming logic to compare two files using the 'While loop'. I suggest you to study carefully this while loop code:
    while(!feof(flp1))
    {
    chl1 = fgetc(flp1);
    if(ferror(fpl1))
    {

    printf("Error reading first file.\n");
    exit(1);
    }

    chl2 = fgetc(flp2);

    if(ferror(flp2))
    {

    printf("Error reading second file.\n");
    exit(1);
    }

    if(chl1 != chl2)
    {
    printf("Files differ at byte number %lu", l);
    same = 0;
    break;
    }

    lp++;

    }

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: C program to Compare two files

    Hi friend,

    Use following method code which compares two files:
    int cmpfls(void)
    {
    FILE *fmp1.*fmp2;
    fp1 fopen(fileNm1 'r'); // open the file1
    fp2 fopen(fileNm1 'r'); // open the file2

    char co1[15] co2[15];

    fgets(co1 16 fmp1);
    fgets(co2 16 fmp2);

    if(strcmp(co1 co2) 0)
    {
    return 1;
    }

    else return 0;
    }

  7. #7
    Join Date
    Dec 2010
    Posts
    1

    Exclamation Re: C program to Compare two files

    Hi Friends,
    I need a File Comparison logic friends , my need is
    1)The program should be able to work in an interactive mode, i.e. take
    two separate files, compare and be able to inform the user if
    the program looks like copied
    2)The copiers should not be able to cheat if they just change the
    variable names.
    3)The copiers should not be able to cheat if they add just spaces in
    between the code lines or inside the code line.

    Friends if you have any idea regarding this , reply me ...

    Thankfully
    Anes P.A

Similar Threads

  1. Windows 7: Program Files vs Program Files(x86)
    By Script in forum Operating Systems
    Replies: 5
    Last Post: 01-06-2011, 06:03 PM
  2. How to compare 2 XML files using Java
    By Aanand in forum Software Development
    Replies: 9
    Last Post: 21-05-2010, 11:04 AM
  3. Replies: 4
    Last Post: 30-01-2010, 07:23 PM
  4. Compare lines of 2 txt files in C#
    By !const in forum Software Development
    Replies: 4
    Last Post: 11-04-2009, 05:36 PM
  5. Compare two Excel files
    By Bull50 in forum Software Development
    Replies: 4
    Last Post: 06-04-2009, 07:14 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,250,856.06855 seconds with 17 queries