OldSchoolCode

.com



Removing white space
Ok, we have been concentrating on how to substitute one char with another in the previous tutorials. A question that is asked frequently is "how can I open a file, read through it, remove all white space and display the updated output?". We'll show a couple of quick ways :)
__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE   
   
#include <stdio.h>
#include <conio.h>
#include <string.h>

void main( void )
{
  unsigned char byte;

  //I created a file named test.txt in the root 
  //of the C: drive. Let's open it
  FILE * pSource = fopen("C:\\test.txt","r");

  //** the original test.txt file **
  //read one byte at a time from the file and 
  //print it to the output screen
  while ( fread( &byte, sizeof(unsigned char), 1, pSource) != 0 )
  {
    printf("%c",byte);
  }

  //close our file !
  fclose( pSource );

  //drop down two lines to  
  //display our modified string
  printf("\n\n");

  //reopen our test.txt file
  pSource = fopen("C:\\test.txt","r");

  while ( fread( &byte, sizeof(unsigned char), 1, pSource) != 0 )
  {
    //if the byte read is not equal
    //to a space, print it out
    if(byte != ' ')
    {
       printf("%c",byte);
    }
  }

  //close our file !
  fclose( pSource );

  getch();

return;
}


__________________________________________________________


Here we will use a function to remove the white space and all the numbers.

__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE

#include <stdio.h>
#include <conio.h>
#include <string.h>

//function to determine what is
//not going to be printed
int removeChars( unsigned char *pByte )
{
  //remember the double
  //pipe means OR. if
  //the byte equals any
  //listed here we return '1'
  if (*pByte == ' ' ||
      *pByte == '0' ||
      *pByte == '1' ||
      *pByte == '2' ||
      *pByte == '3' ||
      *pByte == '4' ||
      *pByte == '5' ||
      *pByte == '6' ||
      *pByte == '7' ||
      *pByte == '8' ||
      *pByte == '9')
  {
    return 1;
  }

return 0;
}

void main( void )
{
  unsigned char byte;

  //I created a file named test.txt in the root
  //of the C: drive. Let's open it
  FILE * pSource = fopen("C:\\test.txt","r");

  //** the original test.txt file **
  //read one byte at a time from the file and
  //print it to the output screen
  while ( fread( &byte, sizeof(unsigned char), 1, pSource) != 0 )
  {
    printf("%c",byte);
  }

  //close our file !
  fclose( pSource );

  //drop down two lines to
  //display our modified string
  printf("\n\n");

  //reopen our test.txt file
  pSource = fopen("C:\\test.txt","r");

  while ( fread( &byte, sizeof(unsigned char), 1, pSource) != 0 )
  {
    //Let's call a function
    int result = removeChars( &byte );

    //if '1' is returned we
    //print nothing :)
    if ( result == 1 )
    {
      printf( "" );
    }
    //if '1' is not returned
    //we print out the char
    else
    {
      printf("%c",byte);
    }
  }

  //close our file !
  fclose( pSource );

  getch();

return;
}