我有一个工作要做的C + +,它假设读取一个文件.txt并使用里面的信息。但是,我们的老师给了我们开始的代码来帮助,我真的不明白。我是C++的初学者,所以我一直在寻找它几个小时,但我还没有找到答案,谢谢!不明白这段代码?这是关于阅读一个文件在C++
下面是一段代码:
int main(int argc, const char** argv)
{
std::ifstream* myfile = NULL;
if (argc == 2) {
file = myfile = new std::ifstream(argv[1]);
if (myfile->fail())
std::cerr << "Error at the opening of the file'" << argv[1] << "'" << std::endl;
}
else
std::cerr << "No file name." << std::endl;
while (*file) {
std::string event;
*file >> event;
if (!(*file)) break;
if (event == "recipe") {
std::string namerecipe;
*file >> recipe;
...
洙我不明白这一点?什么是*文件?和文件?它是文件上的指针吗?为什么没有任何功能可以让线路工作呢?为什么“while *文件”应该这样做? 非常感谢!
你知道指针是什么? – immibis
我怀疑这个代码是故意疯狂的。 – user4581301
等一下。你的老师动态地分配'std :: ifstream'吗?要么我错过了一些东西,要么有特殊的教学目的,或者老师不擅长编程。 –