title: How to iterate through a date range in c++
body: <p>I have a folder with csv files. Each csv file is named by a date (eg. 01JAN2013.csv, 02JAN2013.csv ). I have to read the files in the order of their date (start date and end date are known).</p>
  
  <p>So I am trying to loop through the dates, from the start date to the end date, in order to generate the file names.</p>
  
  <p>Currently I am doing:</p>
  
  <pre><code>vector&lt;string&gt; dd{"01", "02", "03", "04", "05", "06", "07", "08", "09","10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20","21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"};
  vector&lt;string&gt; mmm{"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG","SEP", "OCT", "NOV", "DEC"};
  vector&lt;string&gt; yyyy{"2011","2012","2013","2014"};
  
  string filePath=DataFolderPath;
  
  for(int i=0;i&lt;yyyy.size();i++)
  {
      for(int j=0;j&lt;mmm.size();j++)
      {
          for(int k=0;k&lt;dd.size();k++)
          {
              filePath.append(dd[k]);
              filePath.append(mmm[j]);
              filePath.append(yyyy[i]);
              filePath.append(".csv");
          }
      }
  }
  </code></pre>
  
  <p>Definitely ugly, but it gets the job done.</p>
  
  <p>Is there an easier way to loop through dates in C++. Something along the lines of:</p>
  
  <pre><code>for ( currentDate = starDate; currentDate &lt; endDate; currentDate++) {
  
  //Do stuff
  
   }
  </code></pre>
  
  <hr>
  
  <p>UPDATE:</p>
  
  <p>This is the approach that I finally used, combination of both the answers below:</p>
  
  <pre><code>typdef struct tm Time;
  Time startDate=makeDate(3,1,2011);
  Time endDate=makeDate(24,1,2014);
  time_t end=mktime(&amp;endDate);
  
  for(Time date=startDate;end&gt;=mktime(&amp;date);++date.tm_mday)
  {
      char buffer[16];
      strftime(buffer, sizeof(buffer), "%d%b%Y.csv", &amp;date);
  
      std::string filename(buffer);
              //To convert month to CAPS
      std::transform(filename.begin()+2, filename.begin()+5,filename.begin()+2, ::toupper);
  
  
      std::cout &lt;&lt; filename &lt;&lt; "\n";
  }
  </code></pre>
  
  <p>I also used a <code>makeDate</code> helper function based on paddy's answer which return a <code>struct tm</code> instead of <code>time_t</code>:</p>
  
  <pre><code>Time makeDate( int day, int month, int year )
  {
  Time ttm = {0};
  ttm.tm_mday= day;
  ttm.tm_mon= month-1;
  ttm.tm_year= year-1900;
  return ttm;
  }
  </code></pre>