I am trying to understand the code posted earlier; I am working on the same problem. And I wonder if I can ask for a high level explanation of it; such as what ismpty flag does and high level explanation of what is going on in the program please. How are empty lines detected? The code is from deleting empty lines from a file
Here it is:
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("changeme.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;
line=0;
isempty=1;
}
else
if (c=='\n'&& isempty==0)
{
buffer[i]='\0';
fprintf(tmp,"%s",buffer);
i=0;
isempty=1;
}
}
}
filecopy(tmp,f);
fclose(tmp);
}
else
{
printf("Unable to create tmp 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);
}
while(!feof(to))
{
fprintf(to," ");
}
}