if(infile == NULL)

J J 1 Reputation point
2021-10-05T13:59:30.827+00:00

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++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,543 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 48,821 Reputation points
    2021-10-05T14:45:46.707+00:00

    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
    }
    
    0 comments No comments

  2. WayneAKing 4,921 Reputation points
    2021-10-05T18:00:15.313+00:00

    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
    0 comments No comments