Thread: deleting empty lines from a file

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    100

    deleting empty lines from a file

    hello everybody!
    I am experiencing troubles with a simple program which should delete all the empty lines from a given file.
    Here is my code:

    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," ");
    	}
    
    }
    the problem is that file 'tmp' is smaller than 'f' so there is a kind of garbage left in 'f' after calling filecopy().. Of course I understand why this happens.. My problem is just that i cannot find a solution to this.. How could I truncate the rest of f after the copy?
    Are there other better ways to solve this exercise?

    thanks so much for your kind hints! bye

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    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.
    Don't quote me on that... ...seriously

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    100
    Quote Originally Posted by Brad0407 View Post
    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);
    	}
    }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    c=getc(f)

    c should be declared as int in main as well
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    100
    Quote Originally Posted by vart View Post
    c=getc(f)

    c should be declared as int in main as well
    yes it's true i forgot it! thx!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM