c文件输入输出

(s1)

freopen("1.in","r",stdin);
freopen("1.out","w",stdout);//注意不一定要在程序开始时使用,可以在需要时重定向到文件输入输出 
fclose(stdin);
fclose(stdout);

//若在文件输入输出后,想重定向到控制台操作,可以在freopen前记录下标准输入输出的位置
//dup函数需要使用<unistd.h>库
 int fd_stdin=dup(fileno(stdin));
 //freopen与fclose
 fdopen(fd_stdin,"r");
 
 
 int fd_stdout=dup(fileno(stdout));
 fdopen(fd_stdout,"w"); 

(s2)

//使用文件指针完成输入输出
FILE *in,*out;
in=fopen("C:\Users\76310\Desktop\bat\ustc_score.in","r"); 
fscanf(in,"%s %c",s1,ch);
fgehchar();//注意编译器不同fgetchar()可能会从控制台读入,所以滤空格可以在fscanf()中写格式化字符串
//scanf(”“,&n);""中的部分可以起滤去作用,比如a.%d 
fclose(in);
out=fopen("C:\Users\76310\Desktop\bat\ustc_score.out","w");
fprintf(out,"%d",n);
fclose(out);//一定不要忘记fclose 

其他

(scanf())读入字符串会把空格留在缓冲区,所以若要再读入字符,记得滤去空格

原文地址:https://www.cnblogs.com/Liuz8848/p/15509002.html