/*
 编写一个程序,以一个字符和任意文件名作为命令行参数。如果字符后面没有参数,该程序读取标
 准输入:否则,程序依次打开每个文件并报告每个文件中该字符出现的次数。文件名和字符本身也
 要一同报告。程序应包含错误检查,以确定参数数量是否正确和是否能打开文件。如果无法打开文
 件,程序应报告这一情况,然后继续处理下一个文件。
 */
 //13.11-8.exe c 13.11-8.txt 13.11-8-1.txt 13.11-8-2.txt
#include <stdio.h> 
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
int findchar(char ch,char *file);
int main (int argc, char *argv[] )
 {
     int i;
     char ch;
     FILE *fp;
     char str[30];
     
     if (argc < 2)
         exit(EXIT_FAILURE);
     else if(argc == 2)
     {
         printf("输入要查找的字符串:");
         scanf("%s",str);
         strchr(str,argv[1][0]);    
     }
     else
     {
         for(i=2;i<argc;i++)
         {
             printf("%s中有%d个%c\n",argv[2],findchar(argv[1][0],argv[i]),argv[1][0]);
         }        
     }
     return 0;
 }
int findchar(char ch,char *file)
 {
     FILE *fp;
     char c;
     int count=0;
     
     if((fp = fopen(file, "r"))== NULL)
     {
         printf("%s-erro\n",file);
         return 0;
     }
     else
     {
         while((c=getc(fp))!= EOF)
         {
             if(c==ch)
                 count++;
         }
         return count;    
     }
 }