c++程序freopen的用法
答案:2 悬赏:40
解决时间 2021-03-09 21:35
- 提问者网友:涼初透
- 2021-03-08 23:19
#include
#include
using namespace std;
int main()
{
freopen("f://input.txt","r",stdin);
int a, b;
cin >> a >> b;
freopen("f://output.txt", "w", stdout);
cout << a + b;
fclose(stdout);
fclose(stdin);
system("pause");
return 0;
}
在input.txt中有数据在output.txt中是乱码,为什么啊??
最佳答案
- 二级知识专家网友:为你轻狂半世殇
- 2021-03-09 00:09
数据文件必须用 英文 ASCII 写成,不能是 中文编码。
a 和 b 必须是 正确的整型数。
全部回答
- 1楼网友:努力只為明天
- 2021-03-09 00:49
我表示没遇到你的问题,我上传的文件里有代码和a.txt b.txt你运行1.exe会发现一起好好的,我这里贴出代码:
#include
#include
int main(void)
{
char ch;
freopen("a.txt","rt",stdin);
printf("in a.txt:");
while(!feof(stdin))
{
ch = fgetc(stdin);
putchar(ch);
}
printf("\n\n\nin b.txt:");
freopen("b.txt","rt",stdin);
while(!feof(stdin))
{
ch = fgetc(stdin);
putchar(ch);
}
putchar('\n');
freopen("con","r",stdin);
system("pause");
return 0;
}我要说明是最后一句,将sdin重定向回去控制台的标准输入,不然的话system("pause")无法做到暂停的效果。
运行效果:
我要举报
大家都在看
推荐资讯