Remove Trailing Blank of a file
The below C++ function is used to remove the trialing blanks in a given file
C++ Program to Remove Trialing Blank
#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!='
'&&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
",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.",
MB_ICONINFORMATION | MB_OK
);
return 0;
}
while(--argc)notrail(*++argv);
return 0;
}