deleting extra spaces

This is a discussion on deleting extra spaces within the C++ Programming forums, part of the General Programming Boards category; [color=dark-blue] -Hello everyone .. I have this function it is suppose to copy an input file that contains words with ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    25

    Unhappy deleting extra spaces

    [color=dark-blue]-Hello everyone .. I have this function it is suppose to copy an input file that contains words with different numbers of spaces between them to an output file while deleting any extra spaces between the words… so the output file is identical to the input file except it has only one space between each word… I have written this function in two different ways but both have some problems….…[/color]


    -The problem in this function that it writes all the lines in the input file to only a single line in the output file …


    Code:
    void del_extra_spaces ( ifstream& input_file , ofstream& output_file )
    
    {
    	char word[30] ;
    
          while (! input_file.eof() ) 
    	  
    	  {      input_file >> word ;
    		output_file << word ;
    		output_file << " " ;
    	  }
    	   
    }

    -This function writes all the lines in the input file to only a single line in the output file with no space between the words…


    Code:
    void del_extra_spaces ( ifstream& input_file , ofstream& output_file )
    
    {
    	char c ;
    
          input_file.get ( c ) ;
    	while (! input_file.eof() ) 
    	{   
    	 
    	         if  ( ! isspace ( c ) ) 
                
    	           {
    	                output_file.put ( c ) ;
    	                 input_file.get ( c ) ;
    	            }
    
    	          If  ( isspace ( c ) )
    	  
    	               input_file.get ( c ) ;		          
    	    } 
    	
      }
    -Thank you
    have a nice day

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Your first function uses the overloaded operator ">>" which skips past the newline character in the input file, which is why the output is in one line.

    In your second function, you neglect to actually write the space to the file.

    Try this: (I haven't compiled or tried it, but it should work)
    Code:
    void del_extra_spaces ( ifstream& input_file , ofstream& 
    
    output_file )
    {
    	char c ;
    
    	input_file.get ( c ) ;
    	while ( !input_file.eof() ) 
    	{   
    		if  ( c != " " ) 
    		{
    			output_file.put ( c );
    			input_file.get ( c );
    		}
    		else
    		{
    		//We found a space, but we only want one space,
    		//so read all spaces and place one space in 
    
    output file
    			do{
    				input_file.get ( c );
    			}
    			while( c == " " && !input_file.eof() );
    
    			//Are we at the end of the file?
    			if( !input_file.eof() )
    				output_file.put ( " " );
    		}
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deleting spaces in a string
    By bahada in forum C Programming
    Replies: 11
    Last Post: 05-26-2009, 10:07 PM
  2. Changing 3 spaces to tabs
    By dnguyen1022 in forum C Programming
    Replies: 2
    Last Post: 12-21-2008, 11:51 PM
  3. saving a CString to binary file with trailing spaces
    By nineofhearts in forum C++ Programming
    Replies: 12
    Last Post: 01-03-2006, 10:53 AM
  4. Eliminating extra spaces between words
    By learninC in forum C Programming
    Replies: 8
    Last Post: 03-15-2005, 01:20 PM
  5. Handling spaces in command line arguments
    By starkhorn in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2004, 01:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21