两种输入重定向的方法
-
利用<命令
prog < file
上面的命令会将file内容重定向到stdin,prog程序内部可以直接对stdin进行处理,如:
#include <stdio.h>
int main()
{
int c;
while(EOF != (c = getchar())){
putchar(c);
}
}
2. 利用freopen函数
freopen的函数原型如下:
FILE *freopen(const char *path, constchar *mode, FILE *stream);
freopen会以mode模式打开path文件路径,并重定向到stream中。如果将stream设置为stdin,就会将文件内容重定向到stdin中。
#include <stdio.h>
int main()
{
freopen("input.txt", "r", stdin);
int c;
while(EOF != (c = getchar())){
putchar(c);
}
}