Originally Posted by
Brad0407
If you're making a temp copy of the new file anyways, why not just close the file and reopen it for writing "w". That will erase everything.
i solved it in such way!
Thanks a lot!
Code:
#include <stdio.h>
#include <ctype.h>
#define BUFFSIZE 1024
void filecopy(FILE *from, FILE *to);
int main ()
{
FILE *f;
char buffer[BUFFSIZE]={0};
if (f=fopen("xfile3.txt","r+"))
{
char c; int i=0,isempty=1;
FILE *tmp;
if (tmp=tmpfile())
{
while ((c=getc(f))!=EOF)
{
buffer[i++]=c;
if (c!=' '&&c!='\n'&&c!='\t')
{
isempty=0;
}
else
{
if (c=='\n'&&isempty==1)
{
buffer[i]='\0';
i=0;
isempty=1;
}
else
if (c=='\n'&& isempty==0)
{
buffer[i]='\0';
fprintf(tmp,"%s",buffer);
i=0;
isempty=1;
}
}
}
//LAST LINE
if (!isempty)
{
buffer[i]='\0';
fprintf(tmp,"%s",buffer);
}
fclose (f);
if (f=fopen("xfile3.txt","w+"))
{
filecopy(tmp,f);
fclose(tmp);
fclose(f);
}
else
{
printf("Unable to reopen file f.\n");
fclose(tmp);
}
}
else
{
printf("Unable to create temp file.\n");
fclose(f);
}
}
else
{
printf("Unable to open file\n");
}
}
void filecopy(FILE *from, FILE *to)
{
int c;
rewind(from);
rewind(to);
while ((c=getc(from))!=EOF)
{
putc(c,to);
}
}