c++ - 在C++上接收 “Error: expected an identifier”的 “==”

标签 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;
}

最佳答案

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

您可以这样重写:
if (f_in)
    ;
std::ifstream == int NULL);

您会发现这没有任何意义。

也许你的意思是:
if (!f_in) {
    fprintf(stderr, "Can't open input file in.list!\n");
    exit(1);
}

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

关于c++ - 在C++上接收 “Error: expected an identifier”的 “==”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32652355/

相关文章:

c++ - 依赖于尚未构造的对象的构造函数

c++ - msvc 和 clang 中的函数调用不明确,但 gcc 中没有

php - 在 MacOS 中将 ZeroMQ 绑定(bind)到 PHP

perl - $^S 在 Perl 中没有捕获 Eval 死亡

c# - FileStream 慢速,快速读取方式读取许多文件的几个字节

C++ 快速将 int 数组内容转储到文本文件中

c++ - 在C++中从文本文件读取数字行

ruby-on-rails - 模型中的自定义验证

c# - 使用文件流写入字节通过控制台输出进度时出现Out of Memory异常

c# - 如何在 C# 中将大文件拆分成 block ?