今天学文件,练手的...Look
/*
Name: x_type.c
Author: x-bit at 167168.kmip.net(纯技术论坛)
Description: 显示文本文件内容的type命令源代码
dev-cpp5 + win2k 下调试通过
转载请保留上述信息的完整性
Date: 08-11-2004
Copyright: x-bit Allrights Reserved.
*/
#include <stdio.h>
void usage(char *msg)
{
puts(msg);
puts("usage: command filename1 [filename2]..."); /*可显示多个文件内容*/
exit(0);
}
void errMsg(char *msg)
{
puts(msg);
exit(0);
}
void type(int argc, char *argv[])
{
int i;
FILE *fp;
for(i=2; i<=argc; i++) /*循环显示多个文件*/
{
if(fp=fopen(argv[i-1], "r")) /*正常打开文件显示其内容*/
{
putchar('\n');
puts(argv[i-1]);
while(!feof(fp))
putchar(fgetc(fp));
}
else /*错误信息*/
{
putchar('\n');
puts(argv[i-1]);
errMsg("can't find the file.");
}
fclose(fp);
}
}
int main(int argc, char *argv[])
{
if(argc==1)
errMsg("parameter error.");
else
type(argc, argv);
return 0;
}
本文地址:http://com.8s8s.com/it/it26697.htm