File I/O (white-space)

This is a discussion on File I/O (white-space) within the C++ Programming forums, part of the General Programming Boards category; I am wondering how can i erase white space. If program read data from source file then write it on ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    2

    Question File I/O (white-space)

    I am wondering how can i erase white space.
    If program read data from source file then write it on the target file. If there are leading and trailing white-space in the source file but should be discarded for the destination file.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    8

    Talking

    I'm doing something similar. You'll have to build ur write string one character at a time. Use two counters. If you see double/reoccuring spaces, you just skip to the next character in the read string, but maintain the same position in the write string.

    When you write a character into the new string, increase the second counter.

    Hope that helped.

  3. #3
    Registered User ski6ski's Avatar
    Join Date
    Aug 2001
    Posts
    133
    Try:
    Code:
    //Open a stream
    	ifstream file;
    
    //Open file
    	file.open("file.txt", ios::in);
    
    //Read data
    	while(!file.eof())
    	{
    	   file.get(x); //Get the data by char
    	   if(x==' ') //x is a space
                    {
                        //Do nothing
                    }
                        else
                    {
                        //handle file
                    }
            }
    This is taking in every char x; and checking for spaces.
    C++ Is Powerful
    I use C++
    There fore I am Powerful

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  4. White space problems in file input
    By cxs00u in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2002, 11:06 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

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