How do i remove the white spaces in my text file using c program -
how remove white spaces in text file ? reason work behind it, colleague, has lots of text files huge amount of white spaces between them. getting harder him remove them hitting backspace.so planned write code that.and successful upto 99%. check below code have written.
#include<stdio.h> int main() { int b; char array[100]; gets(array); \\ file name given here file *p; file *t; p=fopen(array,"r"); t=fopen("./duplicate.txt","w"); for(;(b=getc(p))!=eof;) { if(b==32) { fputc(b,t); me: b=getc(p); if(b==32) goto me; } fputc(b,t); } }
the code working perfect thing did it, creating duplicate of original text file.but dont want make duplicate , want rewrite original file itself. tried original text file.but going wrong.
i have algorithm how approach saving strings , writing them file think not efficient approach. , guys have doubt
fputs("\b",filepointer);
will these create backspace suppose no answer. because writing backspace character not performing backspace operation.but think there function ungetc() work here.any ideas rewarded.thank you
while agree other posters there better choices c this, , have several security , design problems, i'm going answer original question.
you cannot directly edit file in place, because not changing there - changing length , structure; because of this, @ point will copying: either memory or second file.
malloc()
buffer size of original file (because worst case cannot more, best case smaller) , read characters in memory. when done, fopen()
original file "w" flag , write back. recommend make copy , delete-rename, appears not solution willing accept based on other comments.
a warning, though: during time have reopened file , time have written back, risk losing data.
Comments
Post a Comment