How do I remove whitespace using the >> operator of ifstream?

This is a discussion on How do I remove whitespace using the >> operator of ifstream? within the C++ Programming forums, part of the General Programming Boards category; I am trying to make a program that takes input from a .txt document and copies it to another .txt ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    49

    How do I remove whitespace using the >> operator of ifstream?

    I am trying to make a program that takes input from a .txt document and copies it to another .txt document. I understand that the >> operator of ifstream stops at each whitespace. Is there any way to set it so that it reads through the whitespace? For example, if I wanted to copy this sentence...

    "This is a sentence I would like to copy."

    from one .txt to another .txt, using >> would only copy the "This" string. Is there a way to make it read through the whitespace so it copies the entire sentence? Or do I have to tokenize the sentence in a loop using " " as my delimiter? Here is a watered down version of my code. Thanks.

    Code:
    #include <fstream>
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main(){
        
      string str;
      
      ifstream inputFile ( "input.txt" );  //input.txt contains sentence I want to copy
      inputFile >> str;
      inputFile.close();
      
      ofstream outputFile ( "output.txt" );
      outputFile << str;
      outputFile.close();
    
      return 0; 
    }

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    49
    Thanks Tonto,

    That works for whitespaces, is there anyway to make it read through the newline character, '\n' ? Now the >> operator stops at the end of each sentence. Thanks again.
    Last edited by rakan; 06-18-2006 at 06:43 PM.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    > is there anyway to make it read through the newline character, '\n'
    Well that depends. The fastest thing I can think of is to just read in two lines and concatenate them. If that's not good enough you would have to roll your own input function, which is a bit of work, but can be worth it.

  5. #5
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    How about this? all white space gets stored in the string via this
    approach, as evidenced by the output.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main (void)
    {
    	string a_string;
    	string dummy;
    	char delim;
    	
    	ifstream a_file ("somefile.txt");
    	
    	if (!a_file.is_open ())
    	{
    		cerr << "Cannot open file!" << endl;
    		return 1;
    	}
    	
    	while (a_file >> dummy)
    	{
    		delim = a_file.get ();
    		dummy += delim;
    		a_string += dummy;
    	}
    	
    	cout << a_string;
    	
    	return 0;
    }
    [UPDATE]
    sorry, as Daved pointed out this is flawed, the best thing to do
    would appear to be to read in one character at a time as
    major_small suggests below
    [/UPDATE]
    Last edited by Richie T; 06-19-2006 at 05:02 PM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I believe you can use the rd_buf of the input stream and send it to the output stream to get the whole thing at once. This will work with an output file stream and a stringstream which will get the whole file into a string.

    Richie T's solution won't work if there are multiple whitespace characters in a row.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    for things like this I usually just read in one char at a time...
    Code:
    #include <iostream>
    #include <fstream>
    
    int main()
    {
    	char ch;
    	
    	std::fstream in("test.in",std::ios::in);
    	std::fstream out("test.out",std::ios::out|std::ios::trunc);
    
    	while(in.get(ch))
    	{
    		out<<ch;
    	}
    
    	return 0;
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    49
    Thanks for all your advice guys.

    I ended up writing a function that read the string in a loop and concatamerizing each string into one large string which it would output to a file. I also added a parameter that set a "delimiting" string to tell the loop to stop.

    Major_Small, that's a clever way of doing it. I think that would give me more options to control and edit the data. Hmmmmmm

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    BTW, I found the example. It is rdbuf, not rd_buf:
    Code:
    #include <iostream>
    #include <fstream>
    #include <sstream>
    
    int main()
    {
       std::ifstream file(__FILE__);
       std::ostringstream oss;
       if ( oss << file.rdbuf() )
          std::cout << oss.str();
    }

  10. #10
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Under what sort of circumstances under which oss << file.rdbuf() would fail?

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    An empty file? Or if the file doesn't exist since an error on open isn't checked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to take a string input and remove whitespace?
    By Jasonx521 in forum C Programming
    Replies: 5
    Last Post: 10-06-2006, 11:24 PM

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