Thread: Problem with deleting completely blank lines

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    110

    Problem with deleting completely blank lines

    Hello all...sooo I managed to solve most of the program. This program is suppose to filter out all blank and tabs of the lines I type in, and completely delete all blank lines. However I am unsure of how to do this. It still seems like it is printing the blank line. It does however filter all spaces and tabs. I commented my code as necessary. Help much apprecated!

    Code:
    #include <stdio.h>
    #define UNLIMITED 10000
    
    void blanklessCopy(char to[], char from[]);
    int getline(char line[]);
    
    //prints lines without blanks or tabs
    //delete lines that are completely blank
    main(){
    	int len; //current line length
    	char line[UNLIMITED]; //current input line
    	char newLine[UNLIMITED]; //the new line
    	
    	//while len is greater than 0
    	//means there was a line
    	while ((len = getline(line)) > 0){
    		//copy the line to newLine
    		blanklessCopy(newLine, line);
    		//what to do here?
    		//means it was a blank
    		if(newLine[0] == '\0'){
    			;
    		}
    		//otherwise just print the new
    		//filtered line
    		else{
    			printf("%s", newLine);
    		}
    	}
    }
    
    //copy from into to: assume to is big enough
    //removes all blanks and tabs
    void blanklessCopy(char to[], char from[]){
    	int i, i2;
    
    	i = i2 = 0;
    	while (from[i] != '\0'){
    		if (from[i] == '\t' || from[i] == ' '){
    			i++;
    		}
    		else {
    			to[i2] = from[i];  
    			++i;
    			++i2;
    		}
    	}
    
    	to[i2] = '\0';
    }
    
    //read a line into s, return length
    int getline(char s[]){
    	int c, i;
    
    	for(i = 0; (c = getchar()) != EOF && c != '\n'; ++i)
    		s[i] = c;
    	if(c == '\n'){
    		s[i] = c;
    		++i;
    	}
    	s[i] = '\0';
    	return i;
    }

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    A blank line will consist of a newline and a 0 byte. Thus, checking line[0] == '\n' should fix that for you.

    EDIT: Also, why not use fgets instead of your custom getline function? Does pretty much the same thing.
    Last edited by IceDane; 12-06-2008 at 11:12 PM.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    That is because I am following the K&R book, I haven't got to that section yet. I guess it would be best to learn how to code the given "shortcut" functions, before using them. That way I won't take them for granted. Anyways it worked! Thanks for the help!

  4. #4
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by dnguyen1022 View Post
    That is because I am following the K&R book, I haven't got to that section yet. I guess it would be best to learn how to code the given "shortcut" functions, before using them. That way I won't take them for granted. Anyways it worked! Thanks for the help!
    Consider yourself a notch on my bedpost.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Blank Lines in a menu
    By ColdFire in forum Windows Programming
    Replies: 2
    Last Post: 07-05-2002, 03:51 PM
  2. line reading problem
    By papous in forum C Programming
    Replies: 2
    Last Post: 11-22-2001, 12:29 PM
  3. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM
  4. Having problem deleting node in middle of stack
    By sballew in forum C Programming
    Replies: 3
    Last Post: 10-29-2001, 11:00 AM