Thread: Skipping newline and space Characters In A File

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    11

    Skipping newline and space Characters In A File

    I have an assignment that I`m working on that requires me to read characters from a file and input them into an array. The only problem that I`m having thus far is being able to skip over the '\n' and ' ' (space) characters. Can anyone please help me with this? The function in my program that is giving me trouble is as follows:
    Code:
    void insert(char A[])
    {
    	char nb;
    	int r=0;
    	infile.open("lab05.dat", ios::in);
    	
    	if(!infile)
    	{
    		cerr << "Can not find the file\n";
    		exit(1);
    	}
    	
    	while(!infile.eof())
    	{	
    			infile.get(nb);
    
    				while(nb=='\n')
    				{
    					infile.ignore();
    					infile.get(nb);
    					A[r]=nb;
    					cout << A[r];
    					r++;
    				}
    
    				if(nb==' ')
    				{
    					infile.get(nb);
    					A[r]=nb;
    					cout << A[r];
    					r++;
    				}
    					else
    					{
    						A[r]=nb;
    						cout << A[r];
    						r++;
    					}
    	}
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > skip over the '\n' and ' ' (space) characters.
    So say
    if ( nb != '\n' && nb != ' ' )
    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.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: Skipping '\n' & ' 'Characters In A File

    Looking at just this piece of code, the problems are:
    Code:
    infile.get(nb);    // get a character from the file
    while(nb=='\n')    // loop while the current char is \n
    {
        infile.ignore(); // ignore the next character in the file, the one after the \n
        infile.get(nb);  // get the next character
        A[r]=nb;         // store it, even if it's a \n or space
        cout << A[r];  
        r++;
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    Originally posted by Salem
    > skip over the '\n' and ' ' (space) characters.
    So say
    if ( nb != '\n' && nb != ' ' )
    But wouldn`t this boolean expression require that the character be both '\n' and ' ' at the same time, which shouldn`t happen??

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    say

    nb = 'a'

    ( nb != '\n' && nb != ' ' )
    nb != '\n' is true
    nb != ' ' is also true
    therefore the whole thing is true.

    The whole thing will be false if you have EITHER a '\n' OR a ' '

    Simple boolean logic
    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.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    Originally posted by Salem
    say

    nb = 'a'

    ( nb != '\n' && nb != ' ' )
    nb != '\n' is true
    nb != ' ' is also true
    therefore the whole thing is true.

    The whole thing will be false if you have EITHER a '\n' OR a ' '

    Simple boolean logic
    Sorry, had a brain fart. Probably too much beer already tonight! lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 05-25-2012, 09:27 PM
  2. scanf , char, newline, space
    By Harry Reve in forum C Programming
    Replies: 1
    Last Post: 09-11-2011, 06:49 AM
  3. Cin newline space problem... or something
    By Baqualish in forum C++ Programming
    Replies: 10
    Last Post: 10-17-2007, 03:49 AM
  4. Input from file no white space characters
    By Dan17 in forum C++ Programming
    Replies: 5
    Last Post: 05-09-2006, 08:28 AM
  5. Newline characters
    By xRandyx in forum C Programming
    Replies: 3
    Last Post: 11-15-2005, 11:01 AM