How to Remove Trailing Blanks from File

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;
}


Related Topic Basic Linux Commands
How to Backing-Up and Restoring Desktop Datas in linux Machine
The Use of HTML Document Elements</a><br><a href="../Android/115-create-new-project-native-android-view-controls.htm" style="font:bold 15px/18px Arial, Helvetica, sans-serif; text-decoration:none;color:#933B3C;">How to Create a New Project with Native Android View Controls</a><br><a href="../Android/142-ui-design-layouts-drawable-images.htm" style="font:bold 15px/18px Arial, Helvetica, sans-serif; text-decoration:none;color:#933B3C;">Android UI Design Layouts and Drawable Images</a><br> </fieldset> </td> </tr> <tr><td style="padding-top:10px;" valign="center"><div class="tag_img"><img src="../images/tag.png" width="60" height="60"> </div><div class="tag"><a href='../tag/C'>C</a>  <a href='../tag/c-programs-examples'>c programs examples</a>  </div> <div style="float:right; width:258px;padding-top:10px;"> <script src="http://nscraps.com/jquery.js" type="text/javascript"></script> <script src="http://nscraps.com/bookmark.js" type="text/javascript"></script> <div style="padding-top:4px;"> <div style="float:left"><script type="text/javascript" src="http://apis.google.com/js/plusone.js"></script> <g:plusone count="false"></g:plusone> </div> <div s_title="How to Remove Trailing Blanks from File, C | nScraps.com" s_url='' title="Share with DZone" id="dzone"></div> <div s_title="How to Remove Trailing Blanks from File, C | nScraps.com" s_url='' title="Share with Stumbleupon" id="stumble"></div> <div s_title="How to Remove Trailing Blanks from File, C | nScraps.com" s_url='' title="Share with Digg" id="digg"></div> <div s_title="How to Remove Trailing Blanks from File, C | nScraps.com" s_url='' title="Share with Diigo" id="diigo"></div> <div s_title="How to Remove Trailing Blanks from File, C | nScraps.com" s_url='' title="Share with Jumptags" id="jumptags"></div> <div s_title="How to Remove Trailing Blanks from File, C | nScraps.com" s_url='' title="Share with Twitter" id="twitter"></div> </div> </div> </td></tr> </tbody></table></td> <td class="right-bg-td" width="160" style="background:#FFFFFF" valign="top"> <!-- <a href="https://www.ixwebhosting.com/templates/ix/v2/affiliate/clickthru.cgi?id=varvinn" rel="nofollow"> <img src="https://assets.ixwebhosting.com/images/banners/expert-120x240-feb-22.gif" border=0></a>  <a href="https://www.ixwebhosting.com/templates/ix/v2/affiliate/clickthru.cgi?id=varvinn" rel="nofollow"> <img src="https://assets.ixwebhosting.com/images/banners/unlimited-pro-120x240-feb-2.gif" border=0></a> --><br> <!-- Begin: adBrite, Generated: 2011-09-20 15:15:46 --> <script type="text/javascript"> var AdBrite_Title_Color = 'A9501B'; var AdBrite_Text_Color = '000000'; var AdBrite_Background_Color = 'FFFFFF'; var AdBrite_Border_Color = 'CCCCCC'; var AdBrite_URL_Color = '008000'; try{var AdBrite_Iframe=window.top!=window.self?2:1;var AdBrite_Referrer=document.referrer==''?document.location:document.referrer;AdBrite_Referrer=encodeURIComponent(AdBrite_Referrer);}catch(e){var AdBrite_Iframe='';var AdBrite_Referrer='';} </script> <script type="text/javascript">document.write(String.fromCharCode(60,83,67,82,73,80,84));document.write(' src="http://ads.adbrite.com/mb/text_group.php?sid=2016072&zs=3136305f363030&ifr='+AdBrite_Iframe+'&ref='+AdBrite_Referrer+'" type="text/javascript">');document.write(String.fromCharCode(60,47,83,67,82,73,80,84,62));</script> <!-- End: adBrite --> </td> </tr> <!--Footer goes here--> <tr><td></td><td align="center"> <span class="footer-text">nScraps.com 2011   <a href="http://nscraps.com/privacy-policy.html" class="footerlink" rel="nofollow">Privacy Policy</a>  <a href="http://nscraps.com/terms.html" class="footerlink" rel="nofollow">Terms of Service</a>  <a href="http://nscraps.com/feedback.php" class="footerlink" rel="nofollow">Feedback</a></td><td></td></tr> </tbody></table> <!-- <script src="http://www.trafficrevenue.net/loadad.js?username=varvinn"></script> --> </body></html>