C语言重定向的一点总结

来源:互联网 发布:windows任务计划程序 编辑:程序博客网 时间:2024/06/08 17:42

第一种重定向:利用cmd进行重定向,实现将控制台内容写入到制定文件中。

#include<stdio,h>

int main()

{int ch;

while((ch==getchar())!=EOF)

{putchar(ch);}

return 0;

}.

接下来运行cmd,注意echo.exe 在Debug目录下,最后生成的.txt文件也在这个文件夹下。


接着运行,echo.exe>word.txt 回车

输入 要输入的数据,比如我们输入 hello world

clr+D(EOF) 回车,结束重定向程序。 

第二种方法:利用 fwrite()来做,这个网上

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)

{

FILE * fp;

char fname[50]={"hello world"};
fp=fopen("word.txt","w");
if(fp==NULL)
{printf("can not open file");}
fwrite(fname,1,strlen("hello world"),fp);
fclose(fp);
getchar();
return 0;
}

这个我在运行时,要到Debug文件夹下运行echo.exe才行,也不知为啥,用的是vs2010.

0 0