title: std::getline deal with \n, \r and \r\n
body: <p>I'm reading data in from a text file and do so line by line using <code>std::getline</code>. This by default reads up to a newline character <code>\n</code>. My code relies on this.</p>
  
  <p>However, it turns out I have to deal with data that may be generated in different environments ie where the newline character is <code>\r</code> (Mac), <code>\n</code> (Unix) or <code>\r\n</code> (Windows)</p>
  
  <p>Is there a simple fix so that I can do something like</p>
  
  <pre><code>std::getline(data,str,'\r\n','\r','\n');
  </code></pre>
  
  <p>but get it to first look for <code>\r\n</code> and then <code>\r</code> and <code>\n</code> so that it will return a 'line' independently of whatever environment generated it?</p>
  
  <p><strong>Or more generally: Is there a way of using <code>std::getline</code> to return a 'line' for any type of commonly used line break/carriage return?</strong></p>
  
  <p>At the moment all I have come up with is</p>
  
  <pre><code>std::vector&lt;int&gt; size(3);
  for (int i=0;i&lt;3;i++){
  
      data.open(c_string_file_name);
      switch (i){
          case 0: std::getline(data,str,'\r\n');size[0]=str.size();break;
          case 1: std::getline(data,str,'\r');size[1]=str.size();break;
          case 2: std::getline(data,str,'\n');size[2]=str.size();break;
      }
      data.close();
  
  }
  
  int min=std::min_element(size.begin(),size.end());
  
  std::string delim;
  
  switch (min){
      case 0:delim='\r\n';break;
      case 1:delim='\r';break;
      case 2:delim='\n';break;
  }
  if ((size[0]==size[1])&amp;&amp;(min==1)){
      delim='\r\n';
  }
  //rest of code use
  
  std::getline(data,str,delim.c_str());
  </code></pre>
  
  <p>Based on the assumption that, assuming there are line breaks, the relevant delimiter will produce the shortest 'line'. Also accounting for the fact that <code>\r</code> and <code>\r\n</code> will have the same length string if it is an <code>\r\n</code> delimiter</p>