全部函数太长了。。。。。。。。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main(int argc,char *argv[])
{
void prints(char *agrv[]);
void upper(char *argv[]);
void lower(char *argv[]);
void countline(char *argv[]);
void countword(char *argv[]);
void countchar(char *argv[]);
void backforth(char *argv[]);
int i;
char c;
if (argc==1)
printf("please input the file name\n");
else if (argc==2)
prints(argv);
else if (argc==3){
if (argv[1][0] !='-')
printf("usage:prog -A/a/l/w/c/r filename\n");
else
for (i=1;i<strlen(argv[1]);i++){
c=argv[1][i];
switch (c){
case 'A':upper(argv);break;
case 'a':lower(argv);break;
case 'l':countline(argv);break;
case 'w':countword(argv);break;
case 'c':countchar(argv);break;
case 'r':backforth(argv);break;
default:
printf("find:illegal %c\n",c);
argc=0;
break;
}
}
}
else
printf("usage:prog -A/a/l/w/c/r filename\n");
exit(0);
}
void prints(char *argv[])
{
FILE *fp;
char ch;
fp=fopen(argv[1],"r");
while (!feof(fp)){
ch=fgetc(fp);
fputc(ch,stdout);
}
fclose(fp);
exit(0);
}
void upper(char *argv[])
{
FILE *fp;
char ch;
fp=fopen(argv[2],"r");
if (fp==NULL){
printf("can't open file\n");
exit(0);
}
while (!feof(fp)){
ch=fgetc(fp);
if (ch>='a' && ch<='z')
ch=ch+'A'-'a';
fputc(ch,stdout);
}
fclose(fp);
}
void lower(char *argv[])
{
FILE *fp;
char ch;
fp=fopen(argv[2],"r");
if (fp==NULL){
printf("can't open file\n");
exit(0);
}
while (!feof(fp)){
ch=fgetc(fp);
if (ch>='A' && ch<='Z')
ch=ch-'A'+'a';
fputc(ch,stdout);
}
fclose(fp);
}
void countline(char *argv[])
{
FILE *fp;
char ch;
int count;
count=0;
fp=fopen(argv[2],"r");
if (fp==NULL){
printf("can't open file\n");
exit(0);
}
while (!feof(fp)){
ch=fgetc(fp);
if (ch=='\n')
++count;
}
fclose(fp);
printf("The number of lines is:%d\n",count);
}
void countword(char *argv[])
{
FILE *fp;
char ch;
int state,count;
state=count=0;
fp=fopen(argv[2],"r");
if (fp==NULL){
printf("can't open file\n");
exit(0);
}
while (!feof(fp)){
ch=fgetc(fp);
if (ch==' ' || ch=='\n' || ch=='\t')
state=0;
else if (state==0){
state=1;
++count;
}
}
fclose(fp);
printf("The number of words is:%d\n",count-1);
}
void countchar(char *argv[])
{
FILE *fp;
char ch;
int count;
count=0;
fp=fopen(argv[2],"r");
if (fp==NULL){
printf("can't open file\n");
exit(0);
}
while (!feof(fp)){
ch=fgetc(fp);
if (ch !='\n')
++count;
}
fclose(fp);
printf("The number of chars is:%d\n",count-1);
}
void backforth(char *argv[])
{
FILE *fp;
char ch;
long i;
i=0;
fp=fopen(argv[2],"r");
if (fp==NULL){
printf("can't open file\n");
exit(0);
}
while (!feof(fp))
i++;
i=i-1;
while (i>=0){
fseek(fp,--i,0);
ch=fgetc(fp);
fputc(ch,stdout);
}
fclose(fp);
}
貌似最后一个函数有问题。。。。。。