c++freopen

Read Multiple Files In C++


I am trying to read two files "ListEmployees01.txt" and "ListEmployees02.table". But the program reads only the "ListEmployees01.txt" file and cout is just from that file.

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
using namespace std;

int main()
{
    freopen("ListEmployees01.txt", "r", stdin);
    string s;
    while (getline(cin, s))
        cout << s<<endl;
    fclose(stdin);
    freopen("ListEmployees02.table", "r", stdin);
    while (getline(cin, s))
        cout << s<<endl;

}

Solution

  • You can use std::ifstream instead of changing std::cin's behavior.