CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2010
    Posts
    111

    Compare two file contents

    Hi
    how to compare two text file contents in c++guide me ,

    Thanks
    sona

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Compare two file contents

    Read the contents in two text (char/wchar_t/TCHAR) buffer or two CString objects and then compare these buffers/objects.
    Victor Nijegorodov

  3. #3
    Join Date
    Mar 2010
    Posts
    111

    Re: Compare two file contents

    this program works fine , but , i also check case insensitive for each string which function can i use

    Code:
    int main()
    {
    
    	int i, j=0,k=0;
    
    	string s1,s,newcontent[10],oldcontent[10],addedcont[10];
    
        ifstream updatefile("test.txt");
    
    	ifstream newfile("test2.txt");
    
    	if(updatefile.is_open())
    	{
    		while(!(updatefile.eof()))
    		{
    
    			getline(updatefile,s1);
    
    			oldcontent[j]=s1;
    			j++;
    
    		}
    	}
    
    	else
    	{
    		cout<<"filecannot open";
    
    	}
    	if(newfile.is_open())
    	{
    		while(!(newfile.eof()))
    		{
    
    			getline(newfile,s);
    
    			newcontent[k]=s;
    
    			k++;
    	
    
    		}
    	}
    	else
    	{
    		 cout<<"file cannto open";
    
    	}
    	for(i=0;i<10;i++)
    	{
    		if(newcontent[i].compare(oldcontent[i]) != 0)
    		
    		{
    			addedcont[i]=newcontent[i];
    			cout<<"new"<<addedcont[i];
    		}
    
    		
    	}
    	return 0;
    }
    Last edited by goldenswarna; April 19th, 2010 at 01:50 AM.

  4. #4
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    Re: Compare two file contents

    Dear Goldenswarna,

    after 45 posts: Please use code tags!
    Your code is practically unreadable.

    With regards
    Programartist

  5. #5
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    Re: Compare two file contents

    Dear Goldenswarna,

    thanks for adding code tags.


    Quote Originally Posted by goldenswarna View Post
    this program works fine
    No. It doesn't. Only under certain circumstances it works fine:

    If one (or both) of the files are longer then 10 lines (which was the case in my test) the program will crash. There is no check if your index ("magic" constant 10) overflows.

    Quote Originally Posted by goldenswarna View Post
    , but , i also check case insensitive for each string which function can i use
    string::compare is always case sensitive.
    To change that behaviour convert the strings into their upper case equivalents (or their lower case equivalents) using ctype::tolower (or ctype::toupper).

    With regards
    Programartist
    Last edited by ProgramArtist; April 19th, 2010 at 03:08 AM. Reason: typo

  6. #6
    Join Date
    Apr 2008
    Posts
    133

    Re: Compare two file contents

    Another option for case insensitive comparison is to use the stricmp function

  7. #7
    Join Date
    Mar 2010
    Posts
    111

    Re: Compare two file contents

    thanks sunilcguru and program artist ,how we avoid crash while in that case , is it possible to assign dynamic array?

    if i use stricmp the parameters are passed case to c.str() format or how can i pass?

    Code:
    	if(stricmp(newcontent[i],oldcontent[i])!=0)
    		{
    			addedcont[i]=newcontent[i];
    			cout<<"new"<<addedcont[i];
    		}
    the above code give error , gudie me
    Code:
    i got error char * ...'stricmp' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
            No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    Error executing cl.exe.
    thanks

    sona

  8. #8
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    Re: Compare two file contents

    Hi,

    Quote Originally Posted by goldenswarna View Post
    thanks sunilcguru and program artist ,how we avoid crash while in that case , is it possible to assign dynamic array?
    Instead of using arrays use vectors. Are you familiar with vectors?
    Using dynamically allocated arrays is not a good choice since you don't know the size at the beginning. So you'll need to reallocate them from time to time and that's what vector can do for you.

    Quote Originally Posted by goldenswarna View Post
    if i use stricmp the parameters are passed case to c.str() format or how can i pass?

    Code:
        if(stricmp(newcontent[i],oldcontent[i])!=0)
            {
                addedcont[i]=newcontent[i];
                cout<<"new"<<addedcont[i];
            }
    the above code give error , gudie me
    Code:
    i got error char * ...'stricmp' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
            No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    Error executing cl.exe.
    thanks
    Simple convert your std::string's to char* (with the public member function c_str():

    Code:
        if(stricmp(newcontent[i].c_str(),oldcontent[i].c_str()) != 0)
            {
                addedcont[i] = newcontent[i];
                cout << "new" << addedcont[i] << "\n";
            }
    Another thing:
    I strongly recommend to use spaces between identifiers and operators!

    If someone (like me) has to read
    Code:
    a=var1*var2/var3-var4;
    I lose my head!

    Please write:
    Code:
    a = var1 * var2 / var3 - var4;
    instead!

    With regards
    Programartist

  9. #9
    Join Date
    Mar 2010
    Posts
    111

    Re: Compare two file contents

    Thanks Program Artist

    i m dont know about vectors ? can u guide me , how we use vectors instead of array

    Thanks
    sona

  10. #10
    Join Date
    Apr 2008
    Posts
    133

    Re: Compare two file contents

    Well as ProgramArtist already told you..vectors can grow dynamically at run time. You can find tons of articles on vectors. You can start with this.. http://www.cplusplus.com/reference/stl/vector/

  11. #11
    Join Date
    Mar 2010
    Posts
    111

    Re: Compare two file contents

    Thanks sunil guru

    when i compare two files ,line by line , first time it ll show not matching line? after that when i updated a new line to the file , it ll show all the lines as not matched


    Ex

    file 1

    hai how r u

    file 2


    hai how r u
    hello

    if i execute the above file format it show output as hello


    after i added new line

    file 1

    hell
    hai how r u

    file 2

    hai how r u

    it will show the two lines as not match


    how we will write files as appened at the beginning of the file

    if my question is silly sorry for that

    thanks
    sona
    Last edited by goldenswarna; April 19th, 2010 at 05:17 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured