How to compare two files line by line (C++)

Jan 21, 2011 at 11:25am
Hello.
I have two files. I want to compare them line by line:
first line of first file with first line of second file,
second line of first file with second line of second file,
...etc.

How can I read file by line in C++? I've found fgets, but it's C command, not C++.

Thanks.
Jan 21, 2011 at 12:21pm

C is very nearly a strict subset of C++, so usually if it works in C, it works in C++. You can use fgets so long as you #include <cstdio>

You're right, thought, there's a better way using C++ style.

Try this: http://www.cplusplus.com/reference/iostream/istream/getline/

What do you want to happen when you find the lines to be identical, or different?
Last edited on Jan 21, 2011 at 12:23pm
Jan 22, 2011 at 8:03pm
Thank you, Moschops, for your advice. This code below compare file1.txt and file2.txt line by line until the end of file1.txt:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string.h>

using namespace std;

int main()
{
	fstream file1("file1.txt"), file2("file2.txt");
	char string1[256], string2[256];
	int j; j = 0;
	
	while(!file1.eof())
	{
		file1.getline(string1,256);
		file2.getline(string2,256);
		j++;
		if(strcmp(string1,string2) != 0)
		{
			cout << j << "-th strings are not equal" << "\n";
			cout << "   " << string1 << "\n";
			cout << "   " << string2 << "\n";
		}
	}
        return 0;
}


Now I want to check (before comparison) that file1 and file2 have equal number of lines. This can count the lines:
1
2
3
4
5
6
7
8
        int c = 0;
	string str;
	fstream file3("file2.txt");
	while(!file3.eof())
	{
		getline(file3,str);
		c++;
	}

The problem is that I have to write "file3" to make it work in main() function above. Otherwise, it won't compare file2 with file1. How can I combine this two pieces of code without "file3" ?
Jan 22, 2011 at 8:26pm
after counting the lines, seek to the beginning again.
Jan 23, 2011 at 8:58pm
Thank you, rocketboy9000. Now it works with file2.clear(); file2.seekg(0,ios::beg);:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string.h>

using namespace std;

int main()
{
	ifstream file1,file2;
	file1.open("file1.txt",ios::binary);
	file2.open("file2.txt",ios::binary);
//---------- compare number of lines in both files ------------------
	int c1,c2;
	c1 = 0; c2 = 0;
	string str;
	while(!file1.eof())
	{
		getline(file1,str);
		c1++;
	}
	file1.clear();   //  sets a new value for the error control state
	file1.seekg(0,ios::beg);
	while(!file2.eof())
	{
		getline(file2,str);
		c2++;
	}
	file2.clear();
	file2.seekg(0,ios::beg);
	
 	if(c1 != c2) 
	{
		cout << "Different number of lines in files!" << "\n";
		cout << "file1 has " << c1 << " lines and file2 has " 
				     << c2 << " lines" << "\n";  
	}
//---------- compare two files line by line ------------------
	char string1[256], string2[256];
	int j = 0;
	while(!file1.eof())
	{
		file1.getline(string1,256);
		file2.getline(string2,256);
		j++;
		if(strcmp(string1,string2) != 0)
		{
			cout << j << "-th strings are not equal" << "\n";
			cout << "   " << string1 << "\n";
			cout << "   " << string2 << "\n";
		}
	}
	
	return 0;
}
Jan 23, 2011 at 9:05pm
I still have one "theoretical" question.
To work with files we can use FILE * (an object containing information to control a stream)
or fstream (an input/output file stream class).

In what situations we should choose one or another?
Jan 23, 2011 at 9:26pm
FILE * for binary data
fstream for formated data
Jan 23, 2011 at 10:53pm
Yeah pretty much, but I like my traditional scanf and printf better than fstream. I think there should be a method supporting that syntax rather than the verbose and weird << syntax.
Topic archived. No new replies allowed.