【发布时间】:2023-04-03 01:31:01
【问题描述】:
我正在尝试读取两个文件“ListEmployees01.txt”和“ListEmployees02.table”。但是程序只读取“ListEmployees01.txt”文件,cout 就是来自那个文件。
#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;
}
【问题讨论】:
-
为什么要使用
freopen以C 流stdin的形式打开文件,而不是创建std::ifstreamC++ 对象来读取文件? -
两个
freopen表达式都尝试分配给标准输入,但不能保证可分配。 -
谢谢。我将使用 ifstream