question

JJ-3433 avatar image
0 Votes"
JJ-3433 asked YujianYao-MSFT commented

if(infile == NULL)

Hi,

Just to preface the question: I recently opened a program that I was developing back in 2012. This is the first time I have used visual studio since then so I am a little rusty.

The program was in vs2013. I opened it up and am getting a compiler error. This is in vs2019.

Error = Severity Code Description Project File Line Suppression State
Error C2678 binary '==': no operator found which takes a left-hand operand of type 'std::ifstream' (or there is no acceptable conversion) BlankC++ D:\JSJ\Work - Hedgecalc\Code v2011_10_02\HedgeCalc.cpp 1543


Here is a code snippet. Also there are 10 other places in the code with the same line. Not sure why I am getting an error here and not the other places.


     ifstream infile;
    
     if(infile == NULL)
     {    
         LogFile<<"Error reading file xInitializeRuns.csv"<<endl;
         exit(2);
     }
     else
         LogFile<<"Successfully opened file xInitializeRuns.csv"<<endl;
    
     infile.open(temp_name);
     infile.getline(cMachineName,200);
     infile.getline(store_NumRuns,100);




c++
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,

I suggest you follow their method to solve the error in this line first, and then observe if there are errors in other positions.

0 Votes 0 ·
cooldadtx avatar image
0 Votes"
cooldadtx answered

Up until C++ 11 NULL represented an empty pointer. ifstream supported comparison against NULL so the code worked.

NULL is no longer considered the correct approach for checking as it is really just a preprocessor value set to integer 0 which means the language had to allow pointers to work with arbitrary ints (which can cause issues). The language added the nullptr keyword to replace it and this is the recommended value going forward. With that change ifstream no longer supports the comparison but instead supports conversion to boolean. Hence you just need to change your code a little.

if (!inFile)
{
   //Error
}
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered

ifstream infile;

if(infile == NULL)
{
LogFile<<"Error reading file xInitializeRuns.csv"
<<endl;
exit(2);
}
else
LogFile<<"Successfully opened file xInitializeRuns.csv"
<<endl;

infile.open(temp_name);
infile.getline(cMachineName,200);
infile.getline(store_NumRuns,100);


A footnote observation unrelated to your Subject error:

You are testing for failure of opening the input file
too soon. The test should be done AFTER the infile.open
has been attempted. As written, if the open fails - for
example if the file can't be found - your code will not
produce any error message, the fail bit will be set for
the stream and all attempts to read from the file will
silently be skipped.

The sequence you have of testing right after ifstream
object creation would be fine if you were doing an
implicit open at the same time by specifying the file
name in the constructor rather than via a call to the
open member function:

 ifstream infile(temp_name);
    
 if (infile.fail()) // alternative way to test
 {
     LogFile << "Error reading file xInitializeRuns.csv" << endl;
     exit(2);
 }

  • Wayne

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.