当前位置: 动力学知识库 > 问答 > 编程问答 >

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;

}

网友答案:
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);
}
网友答案:

you could rewrite the statement as

if (f_in); // actually do nothing if the condition is true
std::ifstream == int NULL); { // starts like a variable declaration

that's why the compiler expects an identifier. Next you've another pending error with int NULL (type value). You probably want to write something like:

if (f_in.is_open())

see ifstream ref: http://www.cplusplus.com/reference/fstream/ifstream/

分享给朋友:
您可能感兴趣的文章:
随机阅读: