Thread: comparing two files

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    9

    comparing two files

    hi guys

    i think i have found the solution for the thead i posted a week ago. now i'm actually reading the file as a .txt file. here's the code:

    Code:
    [tag]
    
    #include <iostream>
    #include <conio>
    #include "tokenizer.h"
    
    void menu();
    void option();
    
    int main()
    {
    	char ctr;
    	do
       {
       	clrscr();
          menu();
    
          do
          {
          	cout<<"\n\n\t Proceed with the next test case? (Y/N): \n";
             cin>>ctr;
          }while(ctr!='y' && ctr!='Y' && ctr!='n' && ctr!='N');
       }while(ctr=='y' || ctr=='Y');
    
      return 1;
    }//end main
    
    void menu()
    {
    		cout<<"\n\n\n";
    		cout<<"\n\t\t          * * * * * * * * * * * * * * * * * * * * * * * * * *  \n\n";
       	cout<<"\n\t\t\t                   TESCAGEN: BB vs WB\n";
    		cout<<"\n\n\t\t\t            Press S to Start the Program\n\n";
          cout<<"\n\n\t\t	    	    Press Q to Quit the Program\n";
       	cout<<"\n\t\t          * * * * * * * * * * * * * * * * * * * * * * * * * * \n";
    
          option();
    }
    
    void option()
    {
          char menu;
          ifstream infile;
    
          cout<<"\n\n\t\t\t\t Please enter your choice: ";
          cin>>menu;
    
          if(isalpha(menu))
          switch(menu)
          {
          	case 'S':
             case 's':
    
             infile.open("ass.txt");
    
             string line;
             vector <string> tokens;
             bool endOfFile = infile.eof();
             int count =0;
    
             while(!endOfFile)
             {
             	endOfFile = getStringAndTokens(infile, line, tokens);
    
                if(!endOfFile)
                {
                	count++;
                   cout<<count<<")"<<line<<endl<<endl;
    
                   for(int i=0;i<tokens.size();i++)
                   {
                   	cout<<tokens[i]<<endl;
                   }
    
                   cout<<"\n-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n"<<endl;
    
                   ifstream(infile);
                   system("Pause");
    
                }//end if
    
             }//end while
    
             break;
    
          }//end switch
          else
          	cout<<"\t\t Wrong key in. Please try again.\n\n";
    }
    [/tag]
    i kinda save the identifiers in another text file. let's say for identifier /*1*/, i save it in if.txt

    if(i==1||i==x)

    my question is, how would i compare those two files (ass.txt and if.txt) and then stated that if that identifier is found, in my case a test case will be generated for that identifier? is it possible to compare two files?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    First get the lengths of the files (check out std::ifstream::seekg() and std::ifstream::tellg()) and compare them. If they differ, the files are obviously not the same.

    If they have the same lengths, traverse the file byte per byte and compare them. You probably want to open the file in binary mode (std::ios::bin).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    There is a pre-made program that comes with all windows operating systems. so if you use the following, u might be able to get somewhere, just a thought.
    Code:
    system("comp");


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error opening files in a different dir
    By Ozzie in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2008, 06:55 AM
  2. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM