Search code examples
c++error-handlingfilestreamfileinputstream

Receiving an "Error: expected an identifier" on C++ for "=="


Im receiving an error on the " if (f_in); std::ifstream == int NULL);" code, where the "==" is underlines as the error and is saying "Error: expected as an identifier". I'm not sure on what to do and I am stuck :/. Here is the code:

#include <iostream>     // For cin/cout
#include <fstream>      // For file stream objects (ifstream)
#include <string>       // For the string data object

using namespace std;

int main()
{

   ifstream f_in;   // Creat an object for an 'input' file stream
   string       data;   // An object to hold our data read in form the file


  )

f_in.open("InputData.txt"); 

if (f_in); std::ifstream == int NULL); {
    fprintf(stderr, "Can't open input file in.list!\n");
    exit(1);
}


f_in >> data;
cout << "First data item from file is: ' " << data << endl;

f_in >> data;
cout << "Second data item from file is: ' " << data << endl;

getline(f_in, data);
cout << "Line from file is: '" << data << endl;

getline(f_in, data);
cout << "Next line from file is: '" << data << endl;

// We are finished with the file. We need to close it.
f_in.close();

cout << "Finished!\n";

return 0;
}

Solution

  • if (f_in); std::ifstream == int NULL);
    

    You could rewrite this as this:

    if (f_in)
        ;
    std::ifstream == int NULL);
    

    And you can see that this doesn't really make sense.

    Maybe you meant:

    if (!f_in) {
        fprintf(stderr, "Can't open input file in.list!\n");
        exit(1);
    }
    

    Or

    if (f_in == NULL) {
        fprintf(stderr, "Can't open input file in.list!\n");
        exit(1);
    }