Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

!= operator and file streams

Tags:

c++

#include <iostream>
#include <fstream>

using namespace std;

int main()
{

    ifstream file0( "file0.txt" );
    ifstream file1( "file1.txt" );

    if (file0 != file1) {
        cout << "What is being compared?" << endl;
    }

} 

If the above code, what is being compared in the conditional? I believe it is pointer values but I am unable to find supporting evidence.

Thanks!

like image 756
rshepherd Avatar asked Nov 27 '10 22:11

rshepherd


People also ask

What are file streams in C++?

File streams include two member functions specifically designed to read and write binary data sequentially: write and read . The first one ( write ) is a member function of ostream (inherited by ofstream ). And read is a member function of istream (inherited by ifstream ). Objects of class fstream have both.

What is an output file stream?

An output file stream keeps an internal pointer that points to the position where data is to be written next. The seekp member function sets this pointer and thus provides random-access disk file output. The tellp member function returns the file position.

What is Fin and fout in C++?

Fin is the name of a stream variable and it is used in file stream management. The purpose of both fin and fout is the same as cin and cout in iostream. These two streams are defined in the fstream library. Fin is used to open the file and get the characters from the file and display them.

What is an input file stream?

In an input file stream, characters are moved from the disk into the stream, and your program takes them out from the other end. For output, your program puts characters into the stream, and the system takes them out of the other end and copies them onto the disk.


1 Answers

When doing a comparison on an ifstream the operator void* will be called. If you are using visual studio you can see this if you choose to see the disassembly of the code.

The operator can be found here. As you can see mentioned:

The pointer returned is not intended to be referenced, it just indicates success when none of the error flags are set.

So if both ifstreams fail, they will be equal. If they succeed (although I am not sure where the pointer value comes from) they will not be equal [this has been tested on VS].

like image 66
default Avatar answered Oct 04 '22 15:10

default