Thread: compairing files

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    58

    Question compairing files

    I need to know how i would go about compairing 2 files. I have 2 files and i need to know how to check if the CONTENTS in them are equil. I have tryed putting it in a string the compairing the 2 strings but that dosnt seem to work any one know how i oculd go about this? any help is much apreciated.
    --== www.NuclearWasteSite.com==--

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Something like this -

    Code:
    #include <iostream> 
    #include <fstream>
    
    
    using namespace std;
    
    int main() 
    { 
        ifstream file1("firstfile",ios::binary);
        ifstream file2("secondfile",ios::binary);
    
        char buff1[255]={0};
        char buff2[255]={0};
        bool theSame = 0;
    
        while(!file1.eof() && !file2.eof() && theSame==0)
        {
            file1.read(buff1,sizeof(buff1));
            file2.read(buff2,sizeof(buff2));
    
            if (memcmp(buff1,buff2,sizeof(buff1)))
                theSame=1;
        }
    
        if(theSame==0)
            cout << "Files are the same\n";
        else
            cout << "Files are different\n";
    
    
        return 0;
    } 
    You could optimise it, check the file size before the loop to prevent having to check the whole file and put in some error checking.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    If I remember correctly memcmp will just compare size of memory used but not contents of that memory. Obviously if the size is not the same then the contents can't be the same either, but the size could be the same and the contents different. I think comparison using strcmp() if you use c_style string or the equal operator if you use STL style string or char by char comparison is the way to go if memcmp returns 0. If it isn't working posting your code would be the next step in requesting help.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    58
    hmmm i guess i should have been more spicific, i cant post code because of a security issue, its a fairly big project im working on. Any way, i will try to post code that is altered but still asks the same question. What i am trying to do is write 10 random numbers to two diffrent files. These files are then to be read, and compared. My main problem now is that when i make the 2 copys one file always comes out slightly diffrent than the other, they are in diffrent directorys but the end of one it will have a musical note site, or a # sign and then a letter, (the signs and letters seem to be random) has any one had this problem or know whats going on? i'll post some code to tell you what im doing.


    srand(time(NULL));
    char filename[16];


    int i;
    int b=0;
    int x;
    bool v=0;
    bool y=0;
    char info[50050];
    cout<<"filename>";
    cin>>filename;
    ofstream file1("a:\\filename.txt" ios::trunc);
    ofstream file2(filename, ios::trunc); //
    file2<<filename<<endl;
    file1<<filename<<endl;
    for(i=0; i<10; i++)
    {
    x=0;

    x=rand()%10;
    cout<<".";
    file1<<x;
    file2<<x;
    }


    cout<<"finished";



    file1.close();
    file2.close();

    return 0;
    }


    i have tryed making one file and copying it but for some odd reason file2 always seems to add stuff to the end.
    --== www.NuclearWasteSite.com==--

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > My main problem now is that when i make the 2 copys one file always comes out slightly diffrent than the other,
    Probably because you're using eof() to test for the end of file, and not checking for the EOF result from the function which actually reads the file. As a result, you end up doing one more write (of garbage data) than you ought to do (the read fails with EOF, but you do a write anyway).

    This C code copies files.
    Code:
    void copy_file ( char *from, char *to ) {
        FILE *ifp = fopen( from, "rb" );
        FILE *ofp = fopen( to, "wb" );
        char buff[BUFSIZ];
        int  n;
        while ( (n=fread(buff,1,BUFSIZ,ifp)) > 0 ) {
            fwrite( buff, 1, n, ofp );
        }
        fclose( ifp );
        fclose( ofp );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  2. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  4. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  5. header and source files
    By gtriarhos in forum C Programming
    Replies: 3
    Last Post: 10-02-2005, 03:16 AM