Thread: whitespace eliminator

  1. #1
    Registered User
    Join Date
    Mar 2012
    Location
    Tucson, Arizona, United States
    Posts
    9

    whitespace eliminator

    I'm trying to eliminate any double whitespace, tab, or newline, or any combination of the two in succession from a file (leaving the old one the same and creating a new one). For some reason this seems to just be skipping the else statement where I expected it to write to the new file. the new file is appearing, but there is nothing written into it.

    Code:
     while( fscanf(inputFile, "%c", &currentCharRead) == 1 )
        {
            if((currentCharRead == ' ' || currentCharRead == '	' || currentCharRead == '\n') && 
                (lastCharRead == ' ' || lastCharRead == '	' || lastCharRead == '\n'));
    	else{
            	fprintf(outputFile, "%c", currentCharRead);
            }
            lastCharRead = currentCharRead;
        }
    What am I doing wrong?

    thanks
    Last edited by Salem; 09-07-2012 at 01:53 AM. Reason: folded long line

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    1. You should write your tab as '\t'

    2. Show us how you opened the output file.
    You say it isn't writing anything, but have you tried using a debugger say to verify whether the else is being executed, even it it doesn't seem to be doing anything.

    If the file wasn't opened properly, this loop could still be working and you wouldn't realise it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new to C...need to eliminate whitespace
    By cakestler in forum C Programming
    Replies: 7
    Last Post: 02-02-2009, 12:41 PM
  2. simple Q on whitespace..
    By iunah in forum C Programming
    Replies: 9
    Last Post: 01-16-2009, 01:38 PM
  3. Return key as whitespace?
    By bwisdom in forum C++ Programming
    Replies: 5
    Last Post: 02-10-2008, 04:26 PM
  4. fscanf whitespace
    By BlkR in forum C Programming
    Replies: 10
    Last Post: 04-23-2007, 10:23 AM
  5. whitespace
    By 182 in forum C++ Programming
    Replies: 27
    Last Post: 02-18-2006, 09:50 PM