Squeeze Whitespace to Single Spaces in a Text File - C++ File Stream

C++ examples for File Stream:Text File

Description

Squeeze Whitespace to Single Spaces in a Text File

Demo Code

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char** argv) {
   ifstream in("main.cpp");
   ofstream out("target.cpp");

   if (!in || !out)
      return(EXIT_FAILURE);

   string tmp;//from  w  w w.j  a v a2  s.  com

   in >> tmp;          // Grab the first word
   out << tmp;         // Dump it to the output stream

   while (in >> tmp) { // operator>> ignores whitespace, so all I have
      out << ' ';      // to do is add a space and each chunk of non-
      out << tmp;      // whitespace
   }

   out.close();
}

Related Tutorials