Return to Snippet

Revision: 21973
at December 27, 2009 09:42 by Zufolek


Initial Code
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

char*mytmpfile="notrail.tmp";

void err(char*text){
 if(!text)text="Can't open file";
 MessageBox(
  0,
  text,
  "NoTrail Error",
  MB_ICONERROR | MB_OK
 );
 exit(1);
}


static inline void replace(char*s){
 remove(s);
 rename(mytmpfile,s);
}


static inline void notrail(char*filename){
 FILE*i,*o;
 unsigned long int saved=0;
 unsigned long int n;
 int c;
 int b;
 i=fopen(filename,"rt");
 if(!i)err(0);
 o=fopen(mytmpfile,"wt");
 if(!o)err(0);
 do{
  c=getc(i);
  if(c==EOF)break;
  if(c==32){
   n=1;
   while((c=getc(i))==32)++n;
   if(c!='\n'&&c!=EOF){
    do putc(' ',o);
    while(--n);
   }else saved+=n;
  }
  b=c!=EOF;
  if(b)putc(c,o);
 }while(b);
 fclose(o);
 fclose(i);
 if(saved){
  char text[32];
  sprintf(text,"Saved %lu bytes\n",saved);
  n=MessageBox(
   0,
   text,
   filename,
   MB_ICONASTERISK | MB_OKCANCEL
  );
  if(n==IDOK){
   replace(filename);
   return;
  }
 }
 remove(mytmpfile);
}


int main(int argc,char**argv){
 if(argc<2){
  MessageBox(
   0,
   "Removes trailing blanks. To use, close this"
   " window and drag and drop one or more"
   " files onto the program's icon.",
   "NoTrail by Zufolek 2009",
   MB_ICONINFORMATION | MB_OK
  );
  return 0;
 }
 while(--argc)notrail(*++argv);
 return 0;
}

Initial URL


Initial Description
Simple prog for removing trailing blanks. Not for use with binary, compressed, or encoded files. Please make backups of your files.

Initial Title
Remove trailing blanks from files

Initial Tags


Initial Language
C