接收"错误:预期标识符"在C ++上为" =="

时间:2015-09-18 12:44:01

标签: c++ error-handling filestream fileinputstream

我收到错误的" if(f_in); std :: ifstream == int NULL);"代码,其中" =="作为错误强调并且说"错误:期望作为标识符"。我不知道该怎么做而且我被卡住了:/。这是代码:

#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;
}

2 个答案:

答案 0 :(得分:0)

==

您可以将其重写为:

intern()

你可以看到这并没有多大意义。

也许你的意思是:

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

或者

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

答案 1 :(得分:0)

您可以将语句重写为

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

这就是编译器需要标识符的原因。 接下来,您将使用int NULL(类型值)发出另一个待处理错误。 你可能想写一些类似的东西:

if (f_in.is_open())

请参阅ifstream ref:http://www.cplusplus.com/reference/fstream/ifstream/