C语言中FILE相关
The C programming Language
文件访问
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp;
void filecopy(FILE *, FILE *);//声明可以放在调用或全局
if (argc == 1)
{ /* no args; copy standard input */
filecopy(stdin, stdout);//宏stdin,stdout
}
else
{
while(--argc > 0)
{
if ((fp = fopen(*++argv, "r")) == NULL)
{
printf("cat: can't open %s\n", *argv);
return 1;
}
else
{
filecopy(fp, stdout);
fclose(fp);
}
}
}
printf("Hello world!\n");
return 0;
}
/* filecopy: copy file ifp to file ofp */
void filecopy(FILE *ifp, FILE *ofp)
{
int c;
while ((c = getc(ifp)) != EOF)
putc(c, ofp);
}
这是书上的代码,感觉写的很好,模仿cat功能。下面用CB演示了一下。
CodeBlocks命令行(windows)
project->set arg
命令提示符cmd