How do i remove the white spaces in my text file ? The reason to work behind it, my colleague, he has lots of text files with huge amount of white spaces between them. Its getting harder for him to remove them by hitting backspace.So what i planned is to write a code to that.And i was successful upto 99%. Check up the below code that i have written.
#include<stdio.h>
int main()
{
int b;
char array[100];
gets(array); \\ file name to be 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 but the thing I did it, by creat开发者_如何学Cing a duplicate of original text file.But i dont want to make duplicate , I want to rewrite to the original file itself. I tried it to the original text file.But some where its going wrong.
I have the algorithm how to approach it like saving the strings and writing them back to file but i think that is not the efficient approach. And guys i have a doubt
fputs("\b",filepointer);
will these create a backspace I suppose no is the answer. because its writing the backspace character not performing the backspace operation.But i think there is a function ungetc() will it work here.Any ideas would be rewarded.Thank you
While I would agree other posters there are much better choices then C for this, and you have several security and design problems, I'm going to answer your original question.
You cannot directly edit the file in place, because you are not simply changing what is there - you are changing the length and the structure; because of this, at some point you will be copying: either to memory or to a second file.
malloc()
a buffer that is the size of the original file (because worst case it cannot be more, best case is much smaller) and read your characters in to memory. When done, fopen()
your original file with the "w" flag and write everything back. I would normally recommend you make a copy and then delete-rename, but that appears to not be what a solution you are willing to accept based on your other comments.
A warning, though: during the time you have reopened your file and the time you have written everything back, you risk losing data.
You have serious issues of security (gets()
), design (goto
) and choice of tools (this is a job for a scripting language, really) to begin with. But to answer you question, copy your duplicate.txt
file over the original file.
You can open the file for reading/writing and give a try:
P=fopen(array,"r+");
See more info about fopen.
I understand that you want to sharpen your C skills, but if you install ruby then you could help your colleague with this one-liner at the command prompt which edits the file in place:
ruby -p -i -e '$_.squeeze!' file.txt
And if you want to make a backup file named file.txt.backup
then:
ruby -p -i.backup -e '$_.squeeze!' file.txt
I think this will even work if you supply multiple file names on the end, so you could get fancy:
ruby -p -i -e '$_.squeeze!' *
or
ruby -p -i -e '$_.squeeze!' `find -name '*.txt'`
You tempted me to write it. Don't look if you don't want spoilers.
https://gist.github.com/1096670
精彩评论