Ifstream question with getline

Hello there,
I am writing a small program for a class assignment. The assignment is to compare 2 text files using ifstream and then to read the files until the .txt files differ and display the line number where they differ.

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
int linecounter(string filename1, string filename2)
{   ifstream infile1, infile2;
    infile1.open(filename1.c_str());   // opens first file 
    infile2.open(filename2.c_str());   // opens second file


    if (infile1.fail()); // tests if file is opened
    { cout << "Error opening " << filename1 << "... " << "\n";
    }
    if (infile2.fail()); // tests if file is opened
    { cout << "Error opening " << filename2 << "... " << "\n";
    }
    
    char string1[256], string2[256]; // declares strings for data stream
    int j = 1; // initializes line count
    while ((!infile1.eof()) || (!infile2.eof())) // tests for end of file
    {
        infile1.getline(string1,256); // gets line from file 1
        infile2.getline(string2,256); // gets line from file 2
        j++; // increments count
       
        if (string1 != string2); // tests for string difference
            {   infile1.close(); // closes files
                infile2.close();
                return j; // returns count
            }
    
    }

    return 1;
}


int main()
{  
    

    string filename1;
    cout << "Please enter the name of the first file... ";
    cin >> filename1;

    string filename2;
    cout << "Please enter the name of the second file... ";
    cin >> filename2;

    int line = linecounter(filename1, filename2);

  
    cout << "The line where the files differ is... " << line << "\n";



   return 0;
}


This is what I have so far. Any hints on where I should go from here? Not looking for a solution. Just a general direction where I should head. Am I using getline correctly?
You are using getline() correctly, but your conditions aren't quite there.

Hmm, actually, try doing a do-while loop:
1
2
3
4
5
6
do
{
  // increase counter
  // read input
  // check input
} while (/* both inputs are working */)

This will have your count be correct (unless you think of the first line being zero).

while ((!infile1.eof()) || (!infile2.eof())) // tests for end of file

This loop continues until both files are done, and is a double negative. You want the loop to stop if either are done:
while (infile1.good() && infile2.good()) //or is it infile.is_good()?

You also can't compare char arrays like that, you need strcmp() (which is in <cstring>). This function returns zero on equality so:
if (strcmp(str1, str2) == 0) // then these strings are the same

"if (infile1.fail());" has a spurious semicolon in the end, remove it
same for if (infile2.fail());
same for if (string1 != string2);
"while not eof" is wrong
reading strings would make more sense than reading arrays
operator != does not work with arrays (but works with strings)
infile1.close() is redundant right before a return

what is your function supposed to return if the files are exactly the same? As written, it returns 1, which is the same as when the files differ in the first line, which is hardly good

I would write that main loop more like

1
2
3
4
5
6
7
8
9
10
    string string1, string2;
    unsigned int j = 0; // initializes line count
    while (getline(infile1, string1) && getline(infile2, string2))
    {
        ++j; // increments count
        if (string1 != string2) // tests for string difference
            return j; // returns count
    }
    std::cout << "The files are the same\n";
    return j; // or perhaps return -1;, something to indicate that there is no difference 
Last edited on
Topic archived. No new replies allowed.