-
April 19th, 2010, 12:58 AM
#1
Compare two file contents
Hi
how to compare two text file contents in c++guide me ,
Thanks
sona
-
April 19th, 2010, 01:34 AM
#2
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
-
April 19th, 2010, 01:39 AM
#3
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.
-
April 19th, 2010, 01:44 AM
#4
Re: Compare two file contents
Dear Goldenswarna,
after 45 posts: Please use code tags!
Your code is practically unreadable.
With regards
Programartist
-
April 19th, 2010, 03:08 AM
#5
Re: Compare two file contents
Dear Goldenswarna,
thanks for adding code tags.
Originally Posted by goldenswarna
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.
Originally Posted by goldenswarna
, 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
-
April 19th, 2010, 03:24 AM
#6
Re: Compare two file contents
Another option for case insensitive comparison is to use the stricmp function
-
April 19th, 2010, 03:30 AM
#7
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
-
April 19th, 2010, 03:42 AM
#8
Re: Compare two file contents
Hi,
Originally Posted by goldenswarna
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.
Originally Posted by goldenswarna
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
-
April 19th, 2010, 04:05 AM
#9
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
-
April 19th, 2010, 04:10 AM
#10
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/
-
April 19th, 2010, 04:50 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|