删除文件空行

来源:互联网 发布:淘宝店装修多少钱 编辑:程序博客网 时间:2024/04/28 15:06

 delbl.c

  1. #include<stdio.h>
  2. #include<ctype.h>
  3. int main(int argc,char **argv)
  4. {
  5.  FILE *fp,*fpout;
  6.  int num,len,i,flag=0;
  7.  char str[9216],c,ones[1],*s,filepath[512]="",filename[256],outfilename[256],tmp
  8. [128],savepath[512],tempfile[512];
  9.  if (argc<2)
  10.   {
  11.    printf("用法:delbl 文本文件路径 输出文件/n");
  12.    return;
  13.   }
  14.  fp=fopen(argv[1],"r");
  15.  if (fp==NULL)
  16.   {
  17.    printf("无法打开%s,请检查你的输入是否有误或你是否有读取该文件的权限!/n",argv[1]);
  18.    exit(-1);
  19.   }
  20.  s=strrchr(argv[1],'//');
  21.  if (s==0)
  22.   {
  23.    strcpy(filename,argv[1]);
  24.   }
  25.  else
  26.   {
  27.    s++;
  28.    strcpy(filename,s);
  29.    len=strlen(argv[1])-strlen(s);
  30.    strncpy(filepath,argv[1],len);
  31.   }
  32.  strcpy(savepath,filepath);
  33.  s=strrchr(filename,'.');
  34.  if (s==0)
  35.   {
  36.    strcat(savepath,filename);
  37.    strcat(savepath,".txt");
  38.   }
  39.  else
  40.   {
  41.    len=strlen(filename)-strlen(s);
  42.    strncpy(tmp,filename,len);
  43.    strcat(savepath,tmp);
  44.    strcat(savepath,".out");
  45.    strcat(savepath,s);
  46.   }     
  47.  if (argc>2)
  48.   {
  49.    strcpy(savepath,argv[2]);
  50.   }
  51.  fpout=fopen(savepath,"w");
  52.  if (fpout==NULL)
  53.   {
  54.    printf("无法创建文件%s,请检查你的输入是否有误或你是否有创建该文件的权限!/n",savepath);
  55.    exit(-1);               
  56.   }
  57.  while(1)
  58.   {
  59.    c=fgetc(fp);
  60.    if (c=='/n' ||c==EOF)
  61.     {
  62.      flag=0;
  63.      strcat(str,"/n");
  64.      len=strlen(str);
  65.      for (i=0;i<len;i++)if (!(isspace(str[i])))flag=1;
  66.      if (flag==1)fputs(str,fpout);
  67.      strcpy(str,"");
  68.     }
  69.    else
  70.     {
  71.      sprintf(ones,"%c",c);
  72.      strcat(str,ones);
  73.     }   
  74.    if (c==EOF)break;
  75.   }
  76.  fclose(fp);
  77.  fclose(fpout);
  78.  printf("删除文本文件空行成功!/n"); 
  79.  return;
  80. }
原创粉丝点击