#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!
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.
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.
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.
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.
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].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With