我有一個工作要做的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'嗎?要麼我錯過了一些東西,要麼有特殊的教學目的,或者老師不擅長編程。 –