将命令行参数作为文件名,顺序复制到标准输出,无文件名时由stdin向stout复制
#include<stdio.h>
#include<stdlib.h>
void filecopy(FILE* ifp, FILE* ofp)
{
int c;
while ((c = gets(ifp)) != EOF)
puts(c, ofp);
}
int main(int argc,char *argv[])
{
FILE* ifp; int i = 0;
char* name = argv[0];
if (argc == 1)
{
filecopy(stdin, stdout);
return 0;
}
while (*(argv+(++i)) != NULL)
if ((ifp = fopen(*(argv+i), "r")) == NULL)
printf("%s,cant open:%s\n", name, *(argv+i));
else
{
filecopy(ifp, stdout); fclose(ifp);
}
return 0;
}
gets是从stdin中读取一行,从文件中读取用fgats()。
puts的参数是读取的字符串变量。