C++: Using ifstream with getline();

前端 未结 4 1793
名媛妹妹
名媛妹妹 2021-02-03 12:34

Check this program

ifstream filein(\"Hey.txt\");
filein.getline(line,99);
cout<

        
4条回答
  •  遥遥无期
    2021-02-03 12:48

    The idiomatic way to read lines from a stream is thus:

    {
        std::ifstream filein("Hey.txt");
    
        for (std::string line; std::getline(filein, line); )
        {
            std::cout << line << std::endl;
        }
    }
    

    Note:

    • No close(). C++ takes care of resource management for you when used idiomatically.

    • Use the free std::getline, not the stream member function.

提交回复
热议问题