08 IO库

 1 #include<iostream>
 2 #include<vector>
 3 #include<string>
 4 #include<fstream>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     ifstream in("test.txt");
10     if (!in)
11     {
12         cerr << "打开文件失败" << endl;
13         exit(0);
14     }
15 
16     vector<string> vec;
17     string str;
18     in >> str;
19     while (getline(in, str))
20         vec.push_back(str);
21 
22     for (auto c : vec)
23         cout << c << endl;
24     return 0;
25 }
#include<iostream>
#include<vector>
#include<string>
#include<fstream>
using namespace std;

int main()
{
    ifstream in("test.txt");
    if (!in)
    {
        cerr << "打开文件失败" << endl;
        exit(0);
    }

    vector<string> vec;
    string str;
    in >> str;
    while (getline(in, str))
        vec.push_back(str);

    for (auto c : vec)
        cout << c << endl;
    return 0;
}

test.txt文件内容:

输出结果:

原文地址:https://www.cnblogs.com/sunbines/p/9404246.html