Search code examples
c++streambinaryfstreamifstream

How to fix "no operator " != " matches these operands"?


When I ran the below program, it gave me an error:

no operator "!=" matches these operands

The error line is while (infile.get(ch) != 0).

#include <iostream>
#include <fstream>
#include <process.h>
using namespace std;

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        cerr << "\nFormat:otype filename";
        exit(-1);
    }
    char ch;
    ifstream infile;
    infile.open(argv[1]);
    if (!infile)
    {
        cerr << "\nCan't open " << argv[1];
        exit(-1);
    }
    while (infile.get(ch) != 0)
        cout << ch;
}

Solution

  • while (infile)
    {
        infile.get(ch);
        cout << ch;
    }
    

    I solved with this way

    and

    while (infile.get(ch))
    cout<<ch;
    

    this way.