我在“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/