1. #include <iostream>
    2. #include <fstream>
    3. using namespace std;
    4. int main() {
    5. ifstream ifs;
    6. ifs.open("text.txt", ios::in);
    7. if (!ifs.is_open()) {
    8. cout << "文件打开失败" << endl;
    9. }
    10. //方法1
    11. /*char buf[1024] = { 0 };
    12. while (ifs >> buf) {
    13. cout << buf << endl;
    14. }*/
    15. //方法2
    16. /*char buf2[1024] = { 0 };
    17. while (ifs.getline(buf2,sizeof(buf2))) {
    18. cout << buf2 << endl;
    19. }*/
    20. //方法3
    21. string buf3;
    22. while (getline(ifs, buf3)) {
    23. cout << buf3 << endl;
    24. }
    25. ifs.close();
    26. return 0;
    27. }