【发布时间】:2019-02-14 04:27:42
【问题描述】:
我正在阅读一本教科书,其中的练习需要从一个文件中复制文本并将其写成与另一个文件等效的小写字母。我似乎找不到仅使用 I/O 流的方法(我在网上找到的大多数解决方案都使用流缓冲区)。
我的代码是这样的
int main()
{
string f_name1, f_name2;
cout << "enter the file names" << '\n';
cin >> f_name1>>f_name2;
ofstream fs{ f_name1 };
ifstream fsi{f_name1};
ofstream fs2{f_name2};
fs << "LoRem ipSUM teXt TaXi";
char ch;
while (fsi.get(ch)) {
fs2 << ch;
}
运行后没有任何内容写入第二个文件 (f_name2)。这只是一个空白文件。
编辑:
这也不行
int main()
{
string f_name1, f_name2;
cout << "enter the file names" << '\n';
cin >> f_name1>>f_name2;
ofstream fs{ f_name1 };
ifstream fsi{f_name1};
ofstream fs2{f_name2};
fs << "LoRem ipSUM teXt TaXi";
char ch;
while (fsi>>ch) {
fs2 << ch;
}
}
【问题讨论】:
-
为什么
f_name1打开了2次。 -
@drescherjm 前面的练习需要分别使用 ifstream 和 ofstream。我重新使用了旧代码。