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

This is a discussion on Skipping '\n' & ' 'Characters In A File within the C++ Programming forums, part of the General Programming Boards category; I have an assignment that I`m working on that requires me to read characters from a file and input them ...

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

    Skipping '\n' & ' '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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,547
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,547
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. Characters in a txt file.
    By tay_highfield in forum C Programming
    Replies: 3
    Last Post: 01-31-2003, 08:19 AM

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