title: Stringstream Line Processing
body: <p>the code below copies a text file into a vector and then streams all the lines in the text file to the clipboard.</p>
  
  <p>I want to copy the text file into a vector and stream all the lines BUT I want to process each line with <code>////code..</code> before the next I am struggling with the loop logic.</p>
  
  <p><strong>E.G</strong></p>
  
  <p>line 1 is loaded into stream and copied to clipboard, now do something to line 1</p>
  
  <p>next></p>
  
  <p>line 2 is loaded into stream and copied to clipboard, now do something to line 2
  and so on..</p>
  
  <hr>
  
  <p><code>input.txt</code> looks like this <strong>:</strong></p>
  
  <pre><code>000000
  000001
  000002
  000003
  000004
  000005
  000006
  000007
  000008
  </code></pre>
  
  <p>So take <code>000000</code> copy it to clipboard run <code>////code..</code> on <code>000000</code> and then do the same to <code>000001</code>, <code>000002</code>, <code>000003</code>, <code>000004</code>.....and so on.</p>
  
  <p>Any advice will be appreciated thanks.</p>
  
  <p><strong>CODE BELOW:</strong></p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;windows.h&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;direct.h&gt;
  #include &lt;conio.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;cstdlib&gt;
  #include &lt;winuser.h&gt;
  #include &lt;cmath&gt;
  #include &lt;iomanip&gt;
  #include &lt;complex&gt;
  #include &lt;iostream&gt;
  #include &lt;sstream&gt;
  #include &lt;iterator&gt;
  
  void toClipboard(HWND hwnd, const std::string &amp;s);
  
  /*
  * It will iterate through all the lines in file and
  * put them in given vector then copy vector to clipboard.
  */
  
  //1. Open file and put each line into a vector.
  bool getFileContent(std::string fileName, std::vector&lt;std::string&gt; &amp; vecOfStrs)
  {
  
      // Open the File
      std::ifstream in(fileName.c_str());
  
      // Check if object is valid.
      if (!in)
      {
          std::cerr &lt;&lt; "Cannot open the File : " &lt;&lt; fileName &lt;&lt; std::endl;
          return false;
  
      }
  
      std::string str;
      // Read the next line from File untill it reaches the end.
      while (std::getline(in, str))
      {
          // Line contains string of length &gt; 0 then save it in vector.
          if (str.size() &gt; 0)
              vecOfStrs.push_back(str);
      }
      // Close The File.
      in.close();
  
      return true;
  
  }
  
  
  //2. Declare clipboard functions at file scope.
  void toClipboard(HWND hwnd, const std::string &amp;s) {
      OpenClipboard(hwnd);
      EmptyClipboard();
      HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, s.size() + 1);
      if (!hg) {
          CloseClipboard();
          return;
      }
      memcpy(GlobalLock(hg), s.c_str(), s.size() + 1);
      GlobalUnlock(hg);
      SetClipboardData(CF_TEXT, hg);
      CloseClipboard();
      GlobalFree(hg);
  }
  
  
  
  int main()
  {
      std::vector&lt;std::string&gt; vecOfStr;
      // Get the contents of file in a vector.
      bool result = getFileContent("input.txt", vecOfStr);
      if (result)
      {
          std::stringstream ss;
          // Populate
          std::copy(vecOfStr.begin(), vecOfStr.end(), std::ostream_iterator&lt;std::string&gt;(ss, "\n"));
  
          ////code..
          //Do something to line 1 of stream
          //Do something to line 2 of stream
          //Do something to line 3 of stream
          //.....
          //
  
          // Display
          std::cout &lt;&lt; ss.str() &lt;&lt; std::endl;
  
          // Copy vector to clipboard.
          size_t len = strlen(ss.str().c_str());
          // Get desktop windows and the call toClipboard.
          HWND hwnd = GetDesktopWindow();
          toClipboard(hwnd, ss.str());
          std::getchar();
      }
  
      return 0;
  } 
  </code></pre>