Comparing two text files.

Here are the directions, I sort of answered a couple out of order, my code won't run though? I am not receiving any error, just blankness. If someone could aid me in tidying up the code, that would be awesome.
First, prompt the user for the names of the two files (2 points). Then open each file (2 points). Remember to check for failure (2 points). Keep reading a line from each of them. Increment a line number counter (2 points). If both inputs fail, print a message indicating that both files are identical (2 points). If one of the inputs fails and the other does not, print a message indicating which file is shorter, then exit (2 points). If the two input lines are different, print the line number counter and both input lines, then exit (2 points). If the two input lines are identical, keep on reading (2 points). Plus 4 points for overall structure and style.

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
56
57
58
59
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <string.h>

using namespace std;



int main()
{
	ifstream Mary1,Mary2;
	Mary1.open("Mary1.txt",ios::binary);
	Mary2.open("Mary2.txt",ios::binary);
	
//---------- compare number of lines in both files ------------------//
	int c1,c2;
	c1 = 0; c2 = 0;
	string str;
	while(!Mary1.eof())
	{
		getline(Mary1,str);
		c1++;
	}
	Mary1.clear();   //  set new value for error control state  //
	Mary2.seekg(0,ios::beg);
	while(!Mary2.eof())
	{
		getline(Mary2,str);
		c2++;
	}
	Mary2.clear();
	Mary2.seekg(0,ios::beg);
	
 	if(c1 != c2) 
	{
		cout << "Different number of lines in files!" << "\n";
		cout << "Mary1 has " << c1 << " lines and Mary2 has " 
				     << c2 << " lines" << "\n";  
	}
//---------- compare two files line by line ------------------//
	char string1[256], string2[256];
	int j = 0;
	while(!Mary1.eof())
	{
		Mary1.getline(string1,256);
		Mary2.getline(string2,256);
		j++;
		if(strcmp(string1,string2) != 0)
		{
			cout << j << "-the strings are not equal" << "\n";
			cout << "   " << string1 << "\n";
			cout << "   " << string2 << "\n";
		}
	}
	
	return 0;
}
Last edited on
I am surprised you aren't receiving any compiler errors for lines 9-11, as it is illegal to have statements like that in the global scope.
Oops, didn't mean to add that, there I fixed it. Now I am getting 49:28: error: ‘strcmp’ was not declared in this scope

Can someone please help me fix my code up to the directions, I've done a fair amount of work learning how to do this, then implementing, and am dying to get this done.
Last edited on
nahla,

You need to reset the file pointer for Mary1, just like you did for Mary2.
I added some basic string-file operation to your code, you should be able to figure out the rest. the only part that was tricky is making sure you use c-style constant strings when you're opening the files with a string type.

I hope this helps,

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <fstream>
#include <string.h>

#include <stdio.h>
#include <string>

using namespace std;

int main()
{
//TODO add lines for inputting files names
//TODO check if they exist
//TODO check if they are the same name
//TODO check if you can open the files

    string file1, file2;
    ifstream Mary1, Mary2;

    file1="Mary1.txt" ;
    file2="Mary2.txt" ;

    Mary1.open( file1.c_str(), ios::binary ); //c_str() returns C-style string pointer
    Mary2.open( file2.c_str(), ios::binary );

    if (!Mary1)
    {cout << "Couldn't open the file  " << file1<<endl;
    return 1;
    }

    if (!Mary2)
    {cout << "Couldn't open the file " << file2 << endl;
    return 1;
    }

//---------- compare number of lines in both files ------------------//
    int c1,c2;
    c1 = 0;
    c2 = 0;
    string str;
    while(!Mary1.eof())
    {
        getline(Mary1,str);
        c1++;
    }
//    Mary1.clear();   //  set new value for error control state  //
//    Mary2.seekg(0,ios::beg);

    while(!Mary2.eof())
    {
        getline(Mary2,str);
        c2++;
    }

    Mary1.clear();  //add
    Mary1.seekg(0,ios::beg);  //add

    Mary2.clear();
    Mary2.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";
        return 1;
    }
//---------- compare two files line by line ------------------//
    char string1[256], string2[256];
    int j = 0, error_count =0;
    while(!Mary1.eof())
    {
        Mary1.getline(string1,256);
        Mary2.getline(string2,256);
        j++;
        if(strcmp(string1,string2) != 0)
        {
            cout << j << "-the strings are not equal " << endl;
            cout << " file1   " << string1 << endl;
            cout << " file2:  " << string2 << endl;
            error_count++;
        }
    }
    if (error_count > 0) {
    cout << "files are diffrent"<< endl;}
    else {cout << "files are the same"<< endl;}

    return 0;
}
Last edited on
So basically, I've got a small part of the problem correct. Your code above is cleaner, I am just unsure of how to do the rest of the problem. My partner is also very confused on the issue as well, again my points of interests are:


If both inputs fail, print a message indicating that both files are identical (2 points).
If one of the inputs fails and the other does not, print a message indicating which file is shorter, then exit (2 points).
If the two input lines are different, print the line number counter and both input lines, then exit (2 points).
If the two input lines are identical, keep on reading (2 points)..


I am unsure on what it means 'if one of the inputs fails and the other does not, print a message indicating which file is shorter, then exit. And actually the rest of the above instructions, HELP PLEASE??

Rest of Code:

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
56
57
58
59
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <string.h>

using namespace std;



int main()
{
	ifstream Mary1,Mary2;
	Mary1.open("Mary1.txt",ios::binary);
	Mary2.open("Mary2.txt",ios::binary);
	
//---------- compare number of lines in both files ------------------//
	int c1,c2;
	c1 = 0; c2 = 0;
	string str;
	while(!Mary1.eof())
	{
		getline(Mary1,str);
		c1++;
	}
	Mary1.clear();   //  set new value for error control state  //
	Mary1.seekg(0,ios::beg);
	while(!Mary2.eof())
	{
		getline(Mary2,str);
		c2++;
	}
	Mary2.clear();
	Mary2.seekg(0,ios::beg);
	
 	if(c1 != c2) 
	{
		cout << "Different number of lines in files!" << "\n";
		cout << "Mary1 has " << c1 << " lines and Mary2 has " 
				     << c2 << " lines" << "\n";  
	}
//---------- compare two files line by line ------------------//
	char string1[256], string2[256];
	int j = 0;
	while(!Mary1.eof())
	{
		Mary1.getline(string1,256);
		Mary2.getline(string2,256);
		j++;
		if(strcmp(string1,string2) != 0)
		{
			cout << j << " the strings are not equal" << "\n";
			cout << "   " << string1 << "\n";
			cout << "   " << string2 << "\n";
		}
	}
	
	return 0;
}
Last edited on
Began re-arranging, please help me:

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <fstream>
#include <string.h>
#include <ostream> 

#include <stdio.h>
#include <string>

using namespace std;

int main()
{


//Open the two files//

string file1, file2;
    ifstream Mary1, Mary2;

    file1="Mary1.txt" ;
    file2="Mary2.txt" ;


    Mary1.open( file1.c_str(), ios::binary ); //c_str() returns C-style string pointer
    Mary2.open( file2.c_str(), ios::binary );
// Check for failures //
    if (Mary1.fail())
    {cout << "Couldn't open the file  " << file1 <<endl;
    return 0;
    }

    if (Mary2.fail())
    {cout << "Couldn't open the file " << file2 << endl;
    return 0;
    }

// Read lines and increment line number counter //

    char string1[256], string2[400];
    int j = 0, error_count =0;
    while(!Mary1.eof())
    {
        Mary1.getline(string1,256);
        Mary2.getline(string2,256);
        j++;
        if(strcmp(string1,string2) != 0)
        {
            cout << j << "- the strings are not equal " << endl;
            cout << " file1:   " << string1 << endl;
            cout << " file2:  " << string2 << endl;
            error_count++;
        }
    }
    if (error_count > 0) {
    cout << "Files are different"<< endl;}
    else {cout << "Files are the same"<< endl;} 




 //---------- compare number of lines in both files ------------------// 
    int c1,c2;
    c1 = 0;
    c2 = 0;
    string str;
    while(!Mary1.eof())
    {
        getline(Mary1,str);
        c1++;
    }
    
    Mary1.clear();  //add
    Mary1.seekg(0,ios::beg);  //add

//    Mary1.clear();   //  set new value for error control state  //
//    Mary2.seekg(0,ios::beg);

    while(!Mary2.eof())
    {
        getline(Mary2,str);
        c2++;
    }

    Mary2.clear();
    Mary2.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";
        return 0;}
    }
nahla,
I'm going to add these hints for you, but please try to take the time to research on file operations and string comparisons. your code is not far away from completion.

I've taken out your code and just put in the parts that you need:
plus put my comments on where your point tally is coming from.
I'm following the instructions to heart eventhough it doesnt make complete sense to me.
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <fstream>
#include <string.h>
#include <ostream>

#include <stdio.h>
#include <string>

using namespace std;

int main()
{
//Open the two files//

    string file1, file2;
    ifstream Mary1, Mary2;

//    file1="Mary1.txt" ;
//    file2="Mary2.txt" ;


    //First, prompt the user for the names of the two files (2 points).
    cout << "Enter the name for 1st file:" << endl;
    cin >> file1;
    cout << "Enter the name for 2nd file:" << endl;
    cin >> file2;

    //Then open each file (2 points)
    Mary1.open( file1.c_str(), ios::binary ); //c_str() returns C-style string pointer
    Mary2.open( file2.c_str(), ios::binary );

    //Remember to check for failure (2 points)
    if (Mary1.fail())
    {
        cout << "Couldn't open the file " << file1 <<endl;
    }

    if (Mary2.fail())
    {
        cout << "Couldn't open the file " << file2 << endl;
    }

    if (file1 == file2)
    {
        cout << "files are identical" << endl;
        return 0;
    }

    //If both inputs fail, print a message indicating that both files are identical (2 points).  this is REALLY what bothers me.
    if (Mary2.fail() && (Mary1.fail()) )
    {
            cout << "both files are identical" << endl;
            return -1;
    }

   //If one of the inputs fails and the other does not, print a message indicating which file is shorter, then exit (2 points).  This makes some sense, but not really.
    if (Mary1.fail() and !Mary2.fail() )
    {
        cout << file1 << " is shorter." << endl;
        return -1;
    }

 if (Mary2.fail() and !Mary1.fail() )
    {
        cout <<file2 << " is shorter." << endl;
        return -1;
    }

    /* YOUR CODE HERE*/
    /*Keep reading a line from each of them. Increment a line number counter (2 points).
    Keep reading a line from each of them. Increment a line number counter (2 points).
    If the two input lines are different, print the line number counter and both input lines,
    then exit (2 points). If the two input lines are identical, keep on reading (2 points).
    */

    return 0;
}
Last edited on
Topic archived. No new replies allowed.