文件输入输出--重定向

来源:互联网 发布:牛奶 知乎 编辑:程序博客网 时间:2024/05/17 00:07

就是一个用来测试文件的读写的程序而已,不过这种方法比fopen好用多了,缺点是不能同时读写文件和标准输入输出。

#define LOCAL //如果要求的是标准输入输出,那就把#define LOCAL删了。
#include<stdio.h>
int main()
{
        #ifdef LOCAL
        freopen("datain.txt","r",stdin);
        freopen("dataout.txt","w",stdout);
        #endif // LOCAL
        int x,n=0,s=0;
        while(scanf("%d",&x)==1)
        {
             s+=x;
             n++;
        }
        printf("%d %d",s,n);
        return 0;
}

本来不想写这篇博客的,看到了百度上有人说把文件放在debug中,实测不行,只能放在程序对应的文件里。(最好不要使用路径)

0 0