/* All of the below was authored by me. By using any part of this source code, you agree to waive all liabilities from the author. Furthermore, you agree that you will not use this source code for personal (or corporate) gain. Finally, should you change any part of this source code, you agree to give credit to the author where such credit is due. */ #include #include #include #define n 5120 void deblank (char *s); // " ABCD EF " --> "ABCD EF" void endnull (char *s); // Ensures strings end with nulls int strdiff(char *s, char *t); int filedif(char *s, char *t); // strdiff returns zero if two strings are identical ----> if (s="abc.txt" && t="path\abc.txt") return 1; // filedif returns zero if two filenames are identical --> if (s="abc.txt" && t="path\abc.txt") return 0; int main() { char infile[n],outfile[n],c1,exit; FILE *in, *out; int i,j; do { /* Get INPUT filename */ do { printf("Input path: "); gets(infile); endnull(infile); deblank(infile); in=fopen (infile, "r"); if (in==NULL) printf ("\nUh oh!\n\n%s cannot be opened.\nMake sure %s exists.\n\n",infile,infile); } while (in==NULL); fflush(stdin); do { printf("Output path: "); gets(outfile); endnull(outfile); deblank(outfile); if(filedif(infile,outfile)) { out=fopen(outfile,"w"); if (out==NULL) printf ("\nUh oh!\n\n%s cannot be created.\n\n",outfile); } else { printf("\nChoose a different output filename!\n\n"); out=NULL; } } while(out==NULL); /* Copy Data */ for(i=0,j=0;!feof(in);i++) { c1 = fgetc(in); if (feof(in)) break; else if(c1!='\n') fputc(c1,out); else j++; } printf("%d NEW-Line characters removed out of %d total characters.\n",j,i); do { printf("\nDo you want to exit (Y/N)?"); exit = getchar(); } while (exit!='Y' && exit!='y' && exit!='N' && exit!='n'); fclose(in); fclose(out); fflush(stdin); } while (exit!='Y' && exit!='y'); return 0; } void deblank (char *s) { char *t=s; for(;*s!='\0';s++) ; s--; for (;*s==' ';s--) *s='\0'; for (s=t;*s==' ';s++) ; for (;*s!='\0';t++,s++) *t=*s; *t='\0'; return; } void endnull (char *s) { int i; for(i=0;i